LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-04-2005, 09:33 AM   #1
skie_knite007
Member
 
Registered: Dec 2004
Location: India
Distribution: Fedora Core 4
Posts: 145

Rep: Reputation: 15
Question How can I know the size of a file using C


Amidst my project (coding), I need to check whether a file is greater than 650MB or not. I'm using C. How can this be done????? Please help me........................
 
Old 04-04-2005, 09:42 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
You can use stat structure with its field st_size

man 2 stat will give you all infos
 
Old 04-04-2005, 10:29 AM   #3
GabeF
Member
 
Registered: Mar 2002
Location: New Hampshire, US
Distribution: Mandrake 8.0, Redhat 8.0
Posts: 101

Rep: Reputation: 15
To my knowledge, there are no standard library functions that directly determine the size of the file. One way of determining file size would be to use fseek() and ftell() and examine the file pointer positions:

Code:
#include <stdio.h>
#include <limits.h>

int main( int argc, char** argv ) {

  FILE* theFile = fopen( argv[1], "r" );  /* Open file, read only */
  long int endPos;

  fseek( theFile, 0, SEEK_END ); /* Place read pointer at an offset of 0 from end of file */

  endPos = ftell( theFile );

  printf( "Pointer position = %ld bytes from file start\n\n", endPos ); 

  printf( "**********************************************\n");
  printf( "Size of long int: %d\n", sizeof(long int) );
  printf( "Maximum value of a long int: %ld\n", LONG_MAX );
  printf( "File size, then, is limited to %ld MB\n", LONG_MAX / 1024 / 1024 );
  printf( "**********************************************\n");
  return 0;

}

On my computer, the output for a 23 byte file:

Pointer position = 23 bytes from file start

**********************************************
Size of long int: 4
Maximum value of a long int: 2147483647
File size, then, is limited to 2047 MB
**********************************************

The size of the file is constrained by the range of a long int, which is system dependent. You said you were checking for a 650MB file, so it shouldn't be a problem. However, if you need more, look into using fsetpos() and fgetpos().

Here's a site that explains fseek(), ftell() and the more capable fsetpos() and fgetpos():

http://www.opengroup.org/onlinepubs/...h/stdio.h.html

<EDIT>
Like I said, to the best of my knowledge. keefaz's suggestion to use stat is much better.
</EDIT>

Last edited by GabeF; 04-04-2005 at 10:49 AM.
 
Old 04-04-2005, 10:49 AM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
A version using stat structure :
Code:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>

int main(int argc, char * argv[]) {
    struct stat * buf;
    int error;

    if(argc == 1) {
        printf("Usage: %s file\n", argv[0]);
        return 1;
    }

    error = stat(argv[1], buf);

    if(error == -1) {
        perror(NULL);
        return 1;
    }

    printf("Size is %ld bytes\n", buf->st_size);
    return 0;
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
when creating a *.iso file, how to make the file size smaller? minm Linux - Newbie 8 12-26-2004 09:58 PM
Getting size of a file yrraja Linux - General 4 12-15-2004 06:18 AM
file system size larger than fysical size:superblock or partition table corrupt klizon Linux - General 0 06-18-2004 04:18 PM
How to get size of file in C Scrag Programming 5 05-20-2004 03:39 PM
get file size in C++ danxl Programming 3 11-18-2003 09:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration