【问题标题】:fscanf is consistently returning 0 when it should be returning 1fscanf 在应该返回 1 时始终返回 0
【发布时间】:2016-12-17 15:06:25
【问题描述】:

我有一个函数,它接收文件流并将存储在该文件中的整数读入一维矩阵。我遇到的问题是我的 fscanf 一直返回 0,而不是像我期望的那样返回 1。我知道我的文件开头的格式正确且符合预期,但我不知道为什么它不会读取第一行。我做错了什么?

/* FUNCTION: readToMatrix
    DESCRIPTION:
        This takes an input stream and reads the file (as described in the header documentation),
        filling the array with the integers contained in the input file stream.
    INPUTS:
        file stream, int *array, matrix width
    OUTPUTS:
        Writes to array
    RETURN:
        Returns 0 on success, nonzero on an unexpected failure.
*/

int readToMatrix( FILE *input, int *array, size_t matWidth )
{
    int x,y;
    long num;

    for ( y = 0; y < matWidth; ++y)
    {
        for ( x = 0; x < matWidth-1; ++x )
        {
            // if fscanf doesn't read 1 number or if EOF then return
            if ( fscanf(input, "%ld,", &num) != 1 || feof(input) ) return -1;
            array[x + y*matWidth] = num;
        }
        if ( fscanf(input, "%ld ", &num) != 1 || feof(input) ) return -1;
        array[x + y*matWidth] = num;
    }   
    return 0;
}

注意:这是输入文件开头的简短 sn-p。

12177,12690,12499,12985,13005,12574,12882,12896,13026,14539,13704,13539,15182,14361,14539,15333,14615,15231,

【问题讨论】:

  • 你在哪里打开你的文件?
  • 文件是在调用它的函数中打开的。
  • 哪个 fscanf 调用?
  • fscanf is consistently returning 0, not 1 like I expect 永远记住,当问题是“标准库中是否存在错误,或者我做错了什么?”答案几乎总是“你做错了什么”。
  • @John3136 当然我做错了什么!只需要弄清楚它是什么。

标签: c matrix file-io scanf


【解决方案1】:

您的循环中有一些小问题。 修复这些,就可以正常运行了:

#include<stdio.h>
int readToMatrix( FILE *input, int *array, size_t matWidth , size_t matHeight)
{
    int x,y;
    long num;
    for ( y = 0; y < matHeight; y++)
    {
        for ( x = 0; x < matWidth; x++ )
        {
            // if fscanf doesn't read 1 number or if EOF then return
            if ( fscanf(input, "%ld,", &num) != 1 || feof(input) ) return -1;    
            array[x + y*matWidth] = num;
        }    
    }       
    return 0;
}
int main()
{
    FILE *fd;
    int matWidth = 5 ;
    int matHeight = 5;
    int array[18]; 
    int i;
    fd=fopen("matrix.txt","r");
    if(fd == NULL)
    {
        printf("open failed!\n");
    }
    else
    {
        readToMatrix( fd, array, matWidth , matHeight);
        fclose(fd);
    }
    for(i=0;i<18;i++)
    {
        printf("%d:%d\n",i,array[i]);
    }
    return 0;
}

这里是matrix.txt:

12177,12690,12499,12985,13005,12574,12882,12896,13026,14539,13704,13539,15182,14361,14539,15333,14615,15231 

输出: 0:12177 1:12690 2:12499 3:12985 4:13005 5:12574 6:12882 7:12896 8:13026 9:14539 10:13704 11:13539 12:15182 13:14361 14:14539 15:15333 16:14615 17:15231

【讨论】:

  • 不,我的循环是正确的。矩阵被限制为正方形,因此它的 x 和 y 维度将相同。
  • @UnclePutin 请试试我这个版本的代码,它可以读","作为分隔符。
【解决方案2】:

我发现了问题。出于某种原因,将文件流传递给函数不起作用。当我让函数创建文件流并在函数本身中打开文件(可能应该如此)时,一切正常。

【讨论】:

  • “不工作”是什么意思?您能否说明自己对自己问题的回答,这不会是“白干活”的事情吗?
猜你喜欢
  • 1970-01-01
  • 2011-02-03
  • 1970-01-01
  • 1970-01-01
  • 2018-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多