简体   繁体   中英

fscanf fails to read data from txt file

I am trying to run my code on eclipse with ubuntu.

I have dumped the data using fprintf into one txt file and reading that file by using 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 :

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(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.

  2. You declared data as char *data and store int 's in it, that is also 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.

  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.

  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 .

  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.

    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.

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"

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.

Also I am not getting why are doing this:

fprintf(fp,"\\n");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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