Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
Not being printed out when using fgets

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")

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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);
over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!