0

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?

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
studinstru
  • 124
  • 12
  • Try flushing and closing file. – Elixir Techne Feb 25 '15 at 12:40
  • If this is C, then you should not cast the result of `malloc`. If this is C++, you should use `std::string` and (generally) C++ I/O. In other words, C != C++ and you should generally tag only the language you are writing / compiling. – crashmstr Feb 25 '15 at 12:45
  • what is the meaning of flushing and closing ? – studinstru Feb 25 '15 at 12:48
  • What is the meaning of `fprintf(fp,"\n");` on a read-only file? – Jongware Feb 25 '15 at 12:55
  • "error in creating dump file" is a (mostly) useless error message. Try: `fp=open(path,mode); if(fp==NULL) {perror(path); exit(1);}` (and use EXIT_FAILURE instead of '1') – William Pursell Feb 25 '15 at 13:14
  • @WilliamPursell,error is not opening dump file ..file is opening properly bt data is zero only after fscanf ...it has to show that data as like the dump.txt data what I have mentioned in my query – studinstru Feb 25 '15 at 13:17
  • @studinstru, I am aware that the question you are asking is not related to the file open. You are not aware that you are handling the error incorrectly. error messages belong on stderr and ought to inform the user of the cause of the problem. – William Pursell Feb 25 '15 at 13:24

2 Answers2

1

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"
Community
  • 1
  • 1
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • @studinstru Your program has more problems. – Iharob Al Asimi Feb 25 '15 at 12:56
  • @studinstru check the answer, I didn't find more problems in the code, if you fix this, it must work. – Iharob Al Asimi Feb 25 '15 at 13:03
  • for me while (fscanf(fp, "%d", &data[index]) == 1) condition is failing – studinstru Feb 25 '15 at 13:11
  • @studinstru no, it might be the data file issue, but now you see why you **must** check the return value of functions, it's certainly not eclipse, although in my opinion eclipse is too heavy for such a simple program, what operating system are you using? – Iharob Al Asimi Feb 25 '15 at 13:19
  • @studinstru What desktop? if it's not `KDE` install [geany](http://www.geany.org/). – Iharob Al Asimi Feb 25 '15 at 15:18
  • ,now I am able to read the data .I got the solution .I had opened the eclipse through docker and trying to access the dump.txt which is outside the docker .So during first attempt I have opend that file by using append mode .SO one dummy file with zero byte created and that same is giving zero results in data .When I changed that file to docker,I am able to get the result .Thanks all for your reply . – studinstru Feb 25 '15 at 15:29
  • @studinstru wierd filename if you are on a linux system, directory separator is `/` not ``\``. And your `fopen()` should have returned `NULL` giving you the correct error message. Check why did that happen. – Iharob Al Asimi Feb 25 '15 at 15:31
0

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");

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
Vagish
  • 2,520
  • 19
  • 32