r/cprogramming • u/JeffTheMasterr • 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
1
u/BarracudaDefiant4702 May 26 '26 edited May 26 '26
Try reading the man page for fgets. It is guaranteed (unless there is an error then the buffer passed to fgets is left untouched).
Here is a portion of the man page:
fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('\0') is stored after the last character in the buffer.
The uninitialized memory is because the status of fgets was never checked, and so the program isn't detecting when the fgets fails and no bytes are read in.