I'm learning about file input/output in C. But there is something I don't understand.
I was about to print the simple sentence in the [words.txt] using fgets, but it doesn't work.
[words.txt] as below:
This speech caused a remarkable sensation among the party.
And my code is :
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *buffer = malloc(100);
memset(buffer, 0, 100);
FILE *fp = fopen("words.txt", "r");
fgets(buffer, sizeof(buffer), fp);
printf("%s\n", buffer);
fclose(fp);
free(buffer);
return 0;
}
The output is "This sp"
I thought there was no problem with my code, but the output result was unexpected.
I mean,
why isn't the whole string printing out?
Also, what is the reason for this output string?
(There seems to be no indication that this code will have 7 character,that is, "This sp")