【问题标题】:fscanf fails to read data from txt filefscanf 无法从 txt 文件中读取数据
【发布时间】:2015-02-25 12:38:27
【问题描述】:

我正在尝试使用 ubuntu 在 eclipse 上运行我的代码。

我已使用 fprintf 将数据转储到一个 txt 文件中,并使用 fscanf 读取该文件。我无法将该值读入数据数组。

下面是我的代码:

#include <stdio.h>      /* printf, scanf, NULL */
#include <stdlib.h>     /* malloc, free, rand */

int main(){
    char* data;
    FILE *fp;
    size_t result;
    data = (char*) malloc (sizeof(char)*(1280*800));//Size of one frame
    if (data==NULL){
        printf("NOt able to allocate memory properly\n");
        exit (1);
    }
    fp = fopen ("\\home\\studinstru\\Desktop\\filedump.txt", "r");
    if(fp==NULL){
        printf("Error in creating dump file\n");
        exit (1);
    }
    for(int m = 0;m<1280;m++){
         for(int n = 0;n<800;n++){
         fscanf(fp,"%d/t",data[m*800 + n]);
     }
   }
    fclose(fp);
    return 0;
}

这是我的 filedump.txt 数据:

79  78  78  77  78  79  81  95
82  81  81  81  82  82  82  82
79  78  78  77  78  79  81  95
82  81  81  81  82  82  82  82
79  78  78  77  78  79  81  95
82  81  81  81  82  82  82  82 ....

你能说出这有什么问题吗?

【问题讨论】:

  • 尝试刷新和关闭文件。
  • 如果这是 C,那么你不应该转换 malloc 的结果。如果这是 C++,您应该使用 std::string 和(通常)C++ I/O。换句话说,C != C++,你通常应该只标记你正在编写/编译的语言。
  • flush和close是什么意思?
  • 只读文件上fprintf(fp,"\n");是什么意思?
  • “创建转储文件时出错”是(大部分)无用的错误消息。尝试:fp=open(path,mode); if(fp==NULL) {perror(path); exit(1);}(并使用 EXIT_FAILURE 而不是 '1')

标签: c file-handling


【解决方案1】:

你的代码有几个问题

  1. 您的fscanf() 格式错误,您传递的是值而不是地址,您应该使用

    fscanf(fp, "%d", &data[n + 800 * m]);
    

    如果您的意思是 "\t"tab 字符,则无论如何都不需要它,并且传递值而不是它的地址是未定义行为,因为 fscanf() 会将值视为指针,并且不太可能指向到一个有效的内存地址,此外,它是未初始化的,这是未定义行为的另一个原因。

  2. 您将data 声明为char *data 并在其中存储int,这也是未定义的行为。

  3. 您必须检查 fscanf() 的返回值,因为如果失败,则该值将未初始化,并且将再次出现未定义行为,并且您将阅读文件末尾,因为你永远不会知道你是否到达了它。

  4. 你正在写入文件并打开它以供阅读,这个

    fprintf(fp, "\n");
    

    错了,你不需要从文件中读取它。

  5. Don't cast the result of malloc() 虽然在这种情况下不会导致问题,但它会提高代码的质量。

  6. 不要使用sizeof(char),它会使您的代码更难阅读,而且完全没有必要,因为标准要求sizeof(char) == 1

  7. 您不需要嵌套循环来读取数据,因为数据的形状无关紧要,因为fscanf() 会忽略所有空白字符。

    通过文件读取并使用计数器在数组中移动就足够了,最后您可以检查读取了多少值以验证数据的完整性。

这是您的代码的固定版本

#include <stdio.h>      /* printf, scanf, NULL */
#include <stdlib.h>     /* malloc, free, rand */

int main()
{
    FILE  *fp;
    size_t index;
    int   *data;

    data = malloc(1280 * 800);
    if (data == NULL)
    {
        printf("NOt able to allocate memory properly\n");
        return 1;
    }

    fp = fopen("\\home\\studinstru\\Desktop\\filedump.txt", "r");
    if (fp == NULL)
    {
        printf("Error in creating dump file\n");
        free(data);

        return 2;
    }

    while (fscanf(fp, "%d", &data[index]) == 1)
    {
        fprintf(stdout, "%d ", data[index]);
        index += 1;
        if (index % 800 == 0)
            printf("\n");
    }

    fclose(fp);
    return 0;
}

注意:我建议使用编译器警告,它们有助于防止愚蠢的错误和其他一些错误,例如 char *data 和读取 int 的内容。

另外,从你的文件路径"\\home\\studinstru\\Desktop\\filedump.txt"看来你是在一个非windows系统上,很可能目录分隔符是/而不是\,所以正确的路径必须是

"/home/studinstru/Desktop/filedump.txt"

【讨论】:

  • @studinstru 你的程序有更多问题。
  • @studinstru 检查答案,我没有在代码中发现更多问题,如果你解决了这个问题,它必须工作。
  • for me while (fscanf(fp, "%d", &data[index]) == 1) 条件失败
  • @studinstru 不,这可能是数据文件的问题,但现在你明白为什么必须检查函数的返回值了,当然不是 Eclipse,虽然在我看来eclipse这么简单的程序太重了,你用的是什么操作系统?
  • @studinstru 什么桌面?如果不是KDE,请安装geany
【解决方案2】:

替换

fscanf(fp,"%d/t",data[m*800 + n]);

fscanf(fp,"%d/t",&data[m*800 + n]);

fscanf() 需要目标变量的地址作为参数,而不是变量本身。

我也不明白为什么要这样做:

fprintf(fp,"\n");

【讨论】:

  • 不走运..我已经尝试过 & ,但我仍然无法在监视窗口中看到数据变量没有更新。它只有零值。
  • 你在 eclipse 中检查过吗...我认为我必须在 eclipse 中运行一些东西 ..我认为在 VS 中它会在 eclipse 中运行 bt nt
  • 我的错误我可以删除 fprintf .. 抱歉,仍然有同样的问题
  • @studinstru 试试我的回答,你有 mor 问题。
  • @iharob,使用您的代码,而 (fscanf(fp, "%d", &data[index]) == 1) 条件对我来说失败了。是日食问题吗??
猜你喜欢
  • 1970-01-01
  • 2013-05-06
  • 1970-01-01
  • 2018-08-21
  • 1970-01-01
  • 2014-03-13
  • 2016-05-28
  • 1970-01-01
  • 2018-11-01
相关资源
最近更新 更多