Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7081059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:52:23+00:00 2026-05-28T06:52:23+00:00

I used the fread function and I am getting a buffer size 4 and

  • 0

I used the fread function and I am getting a buffer size 4 and a “Reading Error”.

Why is my buffer not the fileSize?

Here is my code:

FILE *fp;
char* buffer;
fp = fopen("help.txt","r");
if (fp == NULL){
    fputs("Can't open Help file, Help.txt",stderr);
exit(1);
}
fseek(fp, 0, SEEK_END);
long fileSize = ftell(fp);
rewind(fp);

buffer = (char*) malloc (sizeof(char)*fileSize);
if(buffer == NULL){
    fputs("Memory Allocation Error",stderr);
    exit(2);
}

size_t result = fread(buffer,1,fileSize,fp);
if(result != fileSize){
fputs("Reading error\n",stderr);
printf("File Size : %lu\n",fileSize);
printf("Result : %lu\n",result);
printf("Buffer Size : %u\n",sizeof(buffer));
exit(3);
}

fputs(buffer,stdout);

fclose(fp);
free (buffer);

This is the output when I run the program:

Reading error
File Size : 224
Result : 219
Buffer Size : 4
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:52:23+00:00Added an answer on May 28, 2026 at 6:52 am

    The buffer is allocated correctly. The buffer size is calculated uslng sizeof(char)*fileSize, not sizeof(buffer). You can test if the buffer was allocated or not by verifying if buffer is null. If it’s not, then it successfully allocated the buffer with the size greater than or equal to the size you requested.

    The problem is that you opened the file in text mode. Because of that, Windows will replace all the “\r\n” sequences in your file with “\n” as ANSI C requires. You can either accept this behavior or you can open your file in binary mode using fp = fopen("help.txt","rb"); to read all the contents unaltered.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used this code to read file. But fread function always return 0. What
I want to write multiple structures to a single file. I used fwrite function
I've written a simple library file with a function for reading lines from a
I am reading one Chinese text file (used PHP) with the help of below
While I'm using fread() on a normal text file (for example: ANSI file saved
I'd like to read line from file with fstream (I used this before with
I am trying to use md5 code to calculate checksums of file. Now the
I am trying to read all content from a text file. Here is the
malloc: *** error for object 0x10ee008c0: pointer being freed was not allocated *** set
I'm using code that someone else wrote to load a file format that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.