简体   繁体   中英

fscanf not able to read the values

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]);      
}

testd[i][0] , etc, is an rvalue. What you need is &testd[i][0] .

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM