Code:
#include <stdio.h>
void main() {
FILE *ptr;
char buff[255];
ptr = fopen("Test.txt", "w+");
if (ptr != NULL) {
printf("Success\n");
}
fputs("Hello", ptr);
fgets(buff, 255, (FILE *)ptr);
printf("%s", buff);
fclose(ptr);
}
The file "Text.txt" has the content "Hello" when I opened it, but I just can't print it out with fgets. What did I do wrong here?
You didn't rewind the file before reading. fseek(ptr, 0, SEEK_SET); or rewind(ptr);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With