简体   繁体   English

fscanf无法从txt文件读取数据

[英]fscanf fails to read data from txt file

I am trying to run my code on eclipse with ubuntu. 我正在尝试使用ubuntu在eclipse上运行我的代码。

I have dumped the data using fprintf into one txt file and reading that file by using fscanf. 我已经将使用fprintf的数据转储到一个txt文件中,并使用fscanf读取了该文件。 I am not able to read that values into data array. 我无法将这些值读入数据数组。

Below is my code : 下面是我的代码:

#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;
}

This is my filedump.txt data : 这是我的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 ....

Can you tell what is wrong in this? 你能告诉我这是什么问题吗?

Your code has a couble of problems 您的代码有很多问题

  1. Your fscanf() format is wrong and you are passing the value instead of it's address, you should use 您的fscanf()格式错误,并且您正在传递值而不是其地址,则应使用

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

    if you meant "\\t" whcih is the tab character, it's not needed anyway and passing the value instead of it's address is Undefined Behavior, because fscanf() will treat the value as a pointer, and it's not likely pointing to a valid memory address, moreover, it's unintialized which is another reason for undefined behavior. 如果您的意思是"\\t"tab符,则无论如何都不需要,并且传递值而不是地址是Undefined Behavior,因为fscanf()会将值视为指针,并且不太可能指向有效的内存而且,它是未初始化的,这是未定义行为的另一个原因。

  2. You declared data as char *data and store int 's in it, that is also Undefined Behavior. 您将data声明为char *data并在其中存储int ,这也是Undefined Behavior。

  3. You must check the return value of fscanf() beacuse if it fails, then the value will be uninitialized and there will be once again, Undefined Behavior and also you are going to read past the end of the file because you will never know if you reached it. 您必须检查fscanf()的返回值,因为如果失败,则该值将未初始化,并且将再次出现“未定义行为”,并且您将要读取文件的末尾,因为您将永远不知道是否达到了。

  4. You are writing into the file and you open it for reading, this 您正在写入文件,然后将其打开以进行读取,这

     fprintf(fp, "\\n"); 

    is wrong, you don't need it to read from the file. 是错误的,您不需要它从文件中读取。

  5. Don't cast the result of malloc() though this is not causing problems in this case, it will improve the quality of your code. 尽管在这种情况下这不会引起问题,但是请不要malloc()的结果 ,它将提高代码的质量。

  6. Don't use sizeof(char) it makes your code harder to read and it's completely unnecessary since the standard mandates that sizeof(char) == 1 . 不要使用sizeof(char)这会使您的代码更难阅读,并且由于标准要求sizeof(char) == 1 ,因此完全没有必要。

  7. You don't need the nested loop to read the data, because the shape of the data is irrelevant since fscanf() ignores all whitespace characters. 您不需要嵌套循环即可读取数据,因为fscanf()忽略所有空白字符,因此数据的形状无关紧要。

    It is sufficient to read throug the file and use a counter to move through the array, at the end you can check how many values where read to verify the integrity of the data. 读取文件并使用计数器在数组中移动就足够了,最后,您可以检查读取了多少个值以验证数据的完整性。

This is a fixed version of your code 这是您的代码的固定版本

#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;
}

Note : I recommend the use of compiler warnings, they would help prevent silly mistakes and some other mistakes like char *data and reading int 's into it. 注意 :我建议使用编译器警告,它们将帮助防止愚蠢的错误和其他一些错误,例如char *data并将int读入其中。

Also, from your file path "\\\\home\\\\studinstru\\\\Desktop\\\\filedump.txt" it seems you are on a non-windows system, and very likely the directory separator is / instead of \\ , so the correct path has to be 另外,从您的文件路径"\\\\home\\\\studinstru\\\\Desktop\\\\filedump.txt"看来,您使用的是非Windows系统,并且目录分隔符很可能是/而不是\\ ,因此正确的路径为成为

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

Replace 更换

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

with

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

fscanf() needs address of destination variable as argument and not the variable itself. fscanf()需要将目标变量的地址作为参数,而不是变量本身。

Also I am not getting why are doing this: 另外我不明白为什么要这样做:

fprintf(fp,"\\n");

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

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