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")
CodePudding user response:
Your buffer is declared as a char*
and not a fixed size array so sizeof(buffer)
is 8, hence the 7 characters (plus a '\0'
) being read and printed. sizeof
doesn't know how many bytes you asked to malloc
anyway. I think this is the most confusing thing when you start learning about pointers and realizing arrays work like pointers but not quite.
Solution :
fgets(buffer, 100, fp);
Or
char buffer[100];
fgets(buffer, sizeof(buffer) fp);
CodePudding user response:
Since you declared buffer
as char *buffer
, it is not an array. Instead, it is simply a pointer.
The expression sizeof buffer
will evaluate to the size of the pointer itself, not the size of the object that the pointer is pointing to. Since pointers typically have a size of 32 or 64 bits, the expression sizeof buffer
will evaluate to either 4
or 8
. In your case, it seems to be 8
.
If you had declared buffer
instead like this
char buffer[100];
then buffer
would not be a pointer, but an array, and the expression sizeof buffer
would have evaluated to 100
.
In your case, there is nothing wrong with using malloc
, instead of an array. However, if you use malloc
, you must remember the size of the allocated memory yourself. You cannot use the sizeof
operator for this.
Instead of writing the number 100
in several places in your code, you can use a preprocessor macro for this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ALLOCATION_SIZE 100
int main()
{
char *buffer = malloc( ALLOCATION_SIZE );
memset(buffer, 0, ALLOCATION_SIZE );
FILE *fp = fopen("words.txt", "r");
fgets(buffer, ALLOCATION_SIZE, fp);
printf("%s\n", buffer);
fclose(fp);
free(buffer);
return 0;
}
This has the advantage that if you ever decide to change the number 100
to some other number, you only have to change the number in one place in your code, instead of several places.