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 479537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:48:09+00:00 2026-05-13T00:48:09+00:00

I have a file. I read the size of file. Then I loop reading

  • 0

I have a file. I read the size of file. Then I loop reading two bytes at a time until I get to the end of the file. After every read operation I increment the current position by 2, however the position does not get incremented after I get to half the size of the file, the fread operation will read 0 bytes.

the program reads the file size. I perform fread (2 bytes everytime) until the current position is equal to the size of the file.
It reads 22915 byes for the file size
It increments position by 2 after every read, however when current position gets to 11459 which is half the size of the file it will read zero bytes thus going into an infinite loop.

FILE *file;
char *file_name;
int readCount = 0;
int position = 0;
int fileSize;
unsigned short mem_accesses;

file_name = "sample.txt";

/** open the file */
file = fopen(file_name, "rb");
fseek(file, 0, SEEK_END);
fileSize = ftell(file);
rewind(file);


while(position<fileSize){
   mem_accesses = getNumberAccesses();
   printf("position: %d filesize: %d\n",position, fileSize);

}

unsigned short getNumberAccesses(){
/** calculate number of accesses for process */
unsigned short val;

readCount = fread(&val, sizeof(val), 2, file);

position += readCount;
printf("read count: %d\n", readCount);

return val;
}
  • 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-13T00:48:09+00:00Added an answer on May 13, 2026 at 12:48 am
    readCount = fread(&val, sizeof(val), 2, file);  
    

    This statement reads two items of two bytes each. And it returns the value 2, for the number of items read. The second and third parameters multiplied together tell fread how many bytes to read.

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

Sidebar

Related Questions

This is the loop I have for reading from a file of any size,
Say you have a file of bigger size then you have memory to handle.
I have this very huge XML file of size 2.8GB. This is Polish Wikipedia's
I have a function that allocated a buffer for the size of a file
I have the following code that read input from txt file as follow Paris,Juli,5,3,6
I'm doing some Wave file handling and have them read from disk into an
I have a binary file which I am reading to a collection of byte
I have a really basic problem in c++, I'm reading a tab separated file
I'm trying to read a file into a buffer in blocks of size BLOCK_SIZE
I have a binary file of size about 5MB.. which has lots of interspersed

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.