Reputation: 225
I am trying to read a text file using fscanf.I am working in eclipse in OpenCV on ubuntu. Here are some sample values in the text file 0 5 7 0.547619047619048 1 0.0274509803921569 1 0 6 8 0.541666666666667 1 0.0313725490196078 1 0 8 10 0.533333333333333 1 0.0392156862745098 1
But all fscanf reads is zeros in the array.Here is the part of the code that reads the values
long double testd[1000][6]
FILE* fid1=fopen("file","r");
while((fscanf(fid1,"%Lf",&b))==1)
{
printf("%Lf\n",b);
testsamplecount=testsamplecount+1;
}
for (i=0;i<testsamplecount/6;i++)
{
fscanf(fid1,"%Lf %Lf %Lf %Lf %Lf %Lf",
&testd[i][0],&testd[i][1],
&testd[i][2],&testd[i][3],
&testd[i][4],&testd[i][5]);
}
Upvotes: 0
Views: 714
Reputation: 1
I think you don't move the file pointer to the start point of the file. Use the library fseek(fp, 0, 0) or Try close the file and then open again.
Upvotes: 0
Reputation: 108978
The first loop consumes the file. Try rewind(fid1);
between the loops.
Edit: alternatively, as an option maybe a little more laborious but twice as performant, do a single loop, reading until there is no more data.
Upvotes: 1