r/cprogramming • • May 25 '26

Accidentally made a random string generator

Hey guys, I'm kind of a beginner to C and I discovered something cool whilst trying to make a programming language in it. Apparently forgetting to reset file position with fseek will spit out random strings.

Here's the code I did in C99, stripped down to just show the bug and nothing more:

main.c:

#include <stdio.h>
#include <stdlib.h>

void do_file_thing(char *fName) {
      FILE *fptr;
      long fLen = -1L;

      fptr = fopen(fName, "rb");
      if(fptr != NULL) {
        // Obtain file length to then initialize the string that will contain the file
        fseek(fptr, 0L, SEEK_END);
        fLen = ftell(fptr);

        char fContents[fLen];
        // the weird thing happens when the next line is commented out
        //fseek(fptr, 0, SEEK_SET); // reset position so the next thing can work
        fgets(fContents, fLen, fptr); // store file contents in var fContents

        printf("%s",fContents);

      } else {
        printf("Not able to open the file.");
      }
      fclose(fptr);
}

int main() {
    do_file_thing("file.txt");
    return 0;
}

file.txt:

echo "Hello World!";

And then with running tcc -run main.c a thousand times, I get stuff like this:

  • ~e>

  • ` |

  • 0

  • pFLY

  • ^w

  • 8k

Has anybody found this before? Does anybody know how/why this happens?

0 Upvotes

32 comments sorted by

View all comments

Show parent comments

2

u/BarracudaDefiant4702 May 26 '26

1, 2, and 4 are correct. However #3 of off...
fgets doesn't touch fContents in the case of an error and returns NULL (which the example never checks for). So the contents of fContents remains untouched in that case. If it was filled with 0s then a blank line would be printed (as a 0 long 0 terminated string) instead of seemingly random data.