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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:58:43+00:00 2026-05-20T04:58:43+00:00

I have reduced the problem to the following basic function which should simply print

  • 0

I have reduced the problem to the following basic function which should simply print the
number of bytes in the file.

When I execute it for a file of 83886080 bytes (80 MB) it prints the correct number.
However for a file of 4815060992 bytes (4.48 GB) it prints 520093696 which is way to low.

It seems to have something to do with the SEEK_END option because if I set the pointer to 4815060992 bytes manually (e.g. _fseeki64(fp, (__int64)4815060992, SEEK_SET) _ftelli64 does return the correct position.
So a workaround would be to get the proper file size without using SEEK_END, how is this done?

The code is compiled on a 32 bit Windows system (hence __int64, _iseeki64 and _ftelli64) with MinGW.

In short: what am I doing wrong here?

void printbytes(char* filename)
{
  FILE *fp;
  __int64 n;
  int result;

  /* Open file */
  fp = fopen(filename, "rb");
  if (fp == NULL)
  {
    perror("Error: could not open file!\n");
    return -1;
  }

  /* Find end of file */
  result = _fseeki64(fp, (__int64)0, SEEK_END);
  if (result)
  {
    perror("Error: fseek failed!\n");
    return result;
  }

  /* Get number of bytes */
  n = _ftelli64(fp);

  printf("%I64d\n", n);

  /* Close file */
  fclose(fp);
}
  • 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-20T04:58:44+00:00Added an answer on May 20, 2026 at 4:58 am

    sorry for not posting sooner but I have been preoccupied with other projects for a while.
    The following solution works:

    __int64 nsamples(char* filename)
    {
      int fh;
      __int64 n;
    
      /* Open file */
      fh = _open( filename, _O_BINARY );
    
      /* Find end of file */
      n = _lseeki64(fh, 0, SEEK_END);
    
      /* Close file */
      _close(fh);
    
     return n / sizeof(short);
    }
    

    The trick was using _open instead of fopen to open the file.
    I still don’t understand exactly why this has to be done, but at least this works now.
    Thanks to everyone for your suggestions which eventually pointed me in the right direction.
    (this is a copy of the answer to related question number 4003405).

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

Sidebar

Related Questions

I've reduced my problem (table layout algorithm) to the following problem: Imagine I have
I have the following form, which I have reduced as much as I can,
I have the following controller file: <?php class Test extends CI_Controller { function __construct()
I have the following vector of structs: (defstruct #^{:doc Basic structure for book information.}
This is my Source-XML. It is originally a Word-ML which have been reduced to
I have a problem which has an solution that can be solved by iteration,
i have the following problem Given a string, return a cleaned string where adjacent
How can I reduce resources inside my application? I have tried a number of
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have you determined a maximum number of characters allowed in FCKEditor ? I seem

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.