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 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
  • 3 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

Related Questions

I know that I can figure out today's date by [NSDate date]; but how
I coded my footer ages ago and I can figure out what I did
I'm just combining css files from other templates but I can figure out why
I know im missing something but my friend and I can figure out what.
Maybe it's just too early but I can' figure out why I can't get
I am learning python and have this error . I can figure out where\what
Can't figure out how to use bbp_get_reply_author_role(), and i really need it, returns an
Can I figure out if a function has already been assigned to an event?
Can someone figure out how to get rid of the NOT EXISTS statements in
I can't figure out what this grey text indicates or if it's a glitch

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.