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

  • Home
  • SEARCH
  • 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 64533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:48:17+00:00 2026-05-10T18:48:17+00:00

I am writing a C library that reads a file into memory. It skips

  • 0

I am writing a C library that reads a file into memory. It skips the first 54 bytes of the file (header) and then reads the remainder as data. I use fseek to determine the length of the file, and then use fread to read in the file.

The loop runs once and then ends because the EOF is reached (no errors). At the end, bytesRead = 10624, ftell(stream) = 28726, and the buffer contains 28726 values. I expect fread to read 30,000 bytes and the file position to be 30054 when EOF is reached.

C is not my native language so I suspect I’ve got a dumb beginner mistake somewhere.

Code is as follows:

const size_t headerLen = 54;  FILE * stream; errno_t ferrno = fopen_s( &stream, filename.c_str(), 'r' ); if(ferrno!=0) {   return -1; }  fseek( stream, 0L, SEEK_END ); size_t bytesTotal = (size_t)(ftell( stream )) - headerLen; //number of data bytes to read size_t bytesRead = 0; BYTE* localBuffer = new BYTE[bytesTotal]; fseek(stream,headerLen,SEEK_SET); while(!feof(stream) && !ferror(stream)) {     size_t result = fread(localBuffer+bytesRead,sizeof(BYTE),bytesTotal-bytesRead,stream);     bytesRead+=result; } 

Depending on the reference you use, it’s quite apparent that adding a ‘b’ to the mode flag is the answer. Seeking nominations for the bonehead-badge. 🙂

This reference talks about it in the second paragraph, second sentence (though not in their table).

MSDN doesn’t discuss the binary flag until halfway down the page.

OpenGroup mentions the existance of the ‘b’ tag, but states that it ‘shall have no effect’.

  • 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. 2026-05-10T18:48:18+00:00Added an answer on May 10, 2026 at 6:48 pm

    perhaps it’s a binary mode issue. Try opening the file with 'r+b' as the mode.

    EDIT: as noted in a comment 'rb' is likely a better match to your original intent since 'r+b' will open it for read/write and 'rb' is read-only.

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

Sidebar

Related Questions

I'm writing some data acquisition software and need a gui plotting library that is
I am writing a .NET library that for various reasons cannot be registered in
I'm writing a C++ static library that needs to be shared among several applications,
Let's say that I'm writing a library in C# and I don't know who
I'm learning C# by writing a home library manager. I have a BookController that
I'm writing a program that among other things needs to download a file given
While making some final tests of a class-library that I'm writing for Windows Mobile
I am currently writing a linux application in C that reads from a configuration
I am writing a library in standard C++ which does the phonetic conversion. I
Writing something like this using the loki library , typedef Functor<void> BitButtonPushHandler; throws a

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.