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

Ask A Question

Stats

  • Questions 267k
  • Answers 267k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Add a setTimeout call to the callback function: function callback(data,status)… May 13, 2026 at 12:49 pm
  • Editorial Team
    Editorial Team added an answer Runtime shared libraries RSLs are just SWF files whose code… May 13, 2026 at 12:49 pm
  • Editorial Team
    Editorial Team added an answer Please take a look this post, to unescape htmlentities or… May 13, 2026 at 12:49 pm

Related Questions

I'm writing a straightforward C program on Linux and wish to use an existing
I need to invoke tesseract OCR (its an open source library in C++ that
I have an application where I am reading and writing small blocks of data
I have a C library that needs a callback function to be registered to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.