【问题标题】:fscanf not able to read the valuesfscanf 无法读取值
【发布时间】:2012-01-12 20:17:03
【问题描述】:

我正在尝试使用 fscanf 读取文本文件。我在 ubuntu 上的 OpenCV 中使用 eclipse 工作。 以下是文本文件中的一些示例值 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

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

【问题讨论】:

  • 正如@ouah 在(已删除的)评论中所说,您需要scanf() 中的"%Lf" 转换规范以获取long double"%lf" 用于double
  • 我试过这样做。但它没有读取文件。

标签: c ubuntu file-io scanf


【解决方案1】:

testd[i][0] 等是一个右值。你需要的是&amp;testd[i][0]

【讨论】:

    【解决方案2】:

    第一个循环使用文件。在循环之间尝试rewind(fid1);

    编辑:或者,作为一个选项,可能会更费力但性能提高两倍,执行一个循环,读取直到没有更多数据。

    【讨论】:

      【解决方案3】:

      我认为您不会将文件指针移动到文件的起点。 使用库 fseek(fp, 0, 0) 或 尝试关闭文件,然后再次打开。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-02
        • 1970-01-01
        • 2010-11-06
        相关资源
        最近更新 更多