Number of lines in a text file
Hi all,
I'm having to write a program in C because I need to use some numerical libraries.
I need to read a text file which contain x number of lines with each line having two doubles. For testing I had a dummy file which I knew the length of and hard coded into the program. But now I want to give it any file with having to hard code the number of lines into the program and recompile. Below is the sample code I'm using.
My question is how do I figure out the number of lines in a file. and two is my code below good or should I rework it to run better.
Thanks
Code:
FILE *fp;
char str[80];
char *endpt;
double ld ;
double AllData[1566];
double Xarray[783];
double Yarray[783];
int i = 0;
int j = 0;
if (( fp = fopen("./a/data.dat","r"))==NULL)
{
printf("File not Opened\n");
}
printf("File Opened\n");
while(fscanf(fp,"%lf",&ld)!=EOF)
{
AllData[i]=ld;
// printf("%s\n",str);
i++;
}
j =0;
for(i=0;i<1566;i+=2)
{
Xarray[j]=AllData[i];
j++;
}
j=0;
for(i=1;i<1566;i+=2)
{
Yarray[j]=AllData[i];
j++;
}