Search code examples
cfilefopenfgets

Why does the file change to null when I try to read it?


I am trying to read a file which exists in my project folder. But when I want to get the string in it the file's data changes to (null) . I tried both "w" and "r" but none of them worked. Here is my code :

    FILE* f;
    if (!(f =fopen("a.txt", "r"))) {printf("Could not open file\n"); return;}
    char s[10000];
    fgets(s, 10000, f);
    printf("%s", s);

Output:

(null)
  • Note : There is no error in opening the file.

here is the file after opening

Edit: here is the full version of my code:

    FILE* channelfile;
    char filename[100] , name[]="hi";
    sprintf(filename, "./Resources/Channels/%s.cyko", name);

    if (!(channelfile =fopen("hi.cyko", "r"))) {printf("Could not open file\n"); return;}
    char s[10000];
    fgets(s, 10000, channelfile);
    printf("%s", s);

cyko is my file type (it is not general)

the file destination the original text file


Solution

  • Since there are several people following this question, I will now inform them of the outcome:

    After explaining how to use a debugger to the OP, he was able to find that the actual problem was not in the code he had posted, but rather in another part of his project.

    He assumed that the code he posted was responsible for the problem, because he only experienced the problem after changing the code he posted. However, it seems that this change only triggered the bug; the bug itself was in another part of the program.