April 15th, 2005, 01:45 AM
|
#1 (permalink)
|
| Member
Join Date: Aug 2004 Location: Hyderabad,India
Posts: 242
| What is wrong with this C code ?
HI,
I am trying to read come numbers from a text file and than convert those numbers to floats.Here is my code,Everything looks fine but still I am not able to understand that why it is giving segmentation fault:- Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
main()
{
FILE *fs;
char ch,d;
char str[20];
fs=fopen("text","r"); //I am reading a file :)
int i;
while((ch=fgetc(fs))!=EOF)
{
i=0; // assigning index to 0
if(ch!='\t' ||ch!='\n')
{
str[i]=ch;
i++;
while((ch=fgetc(fs))!='\t')
{
str[i]=ch;
i++;
}
str[i]='\0';
func(str);
}
else
continue;
}
fclose(fs);}
func(char *str)
{
float a;
a=atof(str);
printf("%f",a);
} I hope the code is clear to all
regards |
| |