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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T13:49:46+00:00 2026-05-10T13:49:46+00:00

How can I figure out the size of a file, in bytes? #include <stdio.h>

  • 0

How can I figure out the size of a file, in bytes?

#include <stdio.h>  unsigned int fsize(char* file){   //what goes here? } 
  • 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-10T13:49:47+00:00Added an answer on May 10, 2026 at 1:49 pm

    On Unix-like systems, you can use POSIX system calls: stat on a path, or fstat on an already-open file descriptor (POSIX man page, Linux man page).
    (Get a file descriptor from open(2), or fileno(FILE*) on a stdio stream).

    Based on NilObject’s code:

    #include <sys/stat.h> #include <sys/types.h>  off_t fsize(const char *filename) {     struct stat st;       if (stat(filename, &st) == 0)         return st.st_size;      return -1;  } 

    Changes:

    • Made the filename argument a const char.
    • Corrected the struct stat definition, which was missing the variable name.
    • Returns -1 on error instead of 0, which would be ambiguous for an empty file. off_t is a signed type so this is possible.

    If you want fsize() to print a message on error, you can use this:

    #include <sys/stat.h> #include <sys/types.h> #include <string.h> #include <stdio.h> #include <errno.h>  off_t fsize(const char *filename) {     struct stat st;      if (stat(filename, &st) == 0)         return st.st_size;      fprintf(stderr, "Cannot determine size of %s: %s\n",             filename, strerror(errno));      return -1; } 

    On 32-bit systems you should compile this with the option -D_FILE_OFFSET_BITS=64, otherwise off_t will only hold values up to 2 GB. See the "Using LFS" section of Large File Support in Linux for details.

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

Sidebar

Ask A Question

Stats

  • Questions 83k
  • Answers 83k
  • 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 Reading from the Request.Form collection doesn't cause an exception neither… May 11, 2026 at 4:51 pm
  • Editorial Team
    Editorial Team added an answer I could be wrong, but I think you simply need… May 11, 2026 at 4:50 pm
  • Editorial Team
    Editorial Team added an answer My software business was in a very, very small niche… May 11, 2026 at 4:50 pm

Related Questions

I'm trying to integrate a NSURLConnection object with UIProgressView, so I can update the
I am trying to figure out how to write a new line of text
All, I am trying to load up a bmp file into a GLubyte array
I haven't got a unique problem, but for the life of me I can't

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.