简体   繁体   English

fscanf无法读取值

[英]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. 我正在尝试使用fscanf读取文本文件。我正在ubuntu上的OpenCV中的eclipse中工作。 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 以下是文本文件中的一些示例值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 但是所有fscanf读取的都是数组中的零,这是代码中读取值的部分

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. testd[i][0]等是一个右值。 What you need is &testd[i][0] . 您需要的是&testd[i][0]

The first loop consumes the file. 第一个循环使用文件。 Try rewind(fid1); 尝试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. 使用库fseek(fp,0,0)或尝试关闭文件,然后再次打开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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