Getting file sizes

This is a discussion on Getting file sizes within the C Programming forums, part of the General Programming Boards category; im attempting to get a file's size, with not alot of help. any one know of site with some good ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    913

    Getting file sizes

    im attempting to get a file's size, with not alot of help. any one know of site with some good info, like cplusplus.com's style.

    heres what i was trying.
    Code:
    extern int getfilesize(char *src) {
        struct stat filesize;
        
        stat(src, &filesize);
        
        return (filesize.st_size);
    }
    i created a txt file and ran this, then i copied and pasted some text into the file and the size didnt change.

  2. #2
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    This should help, it came from www.experts-exchange.com
    Code:
    #include<stdio.h>
    #include<sys/stat.h>
    
    int main()
    {
     struct stat buf;
     int result;
     
     result = stat("C:/yourfilename.txt",&buf);
    
    if( result != 0 )
      {
         perror( "Problem getting information" );
      }
      else
     {
       printf( "File size     : %ld\n", buf.st_size );
    }
    return 0;
    }
    Last edited by stumon; 03-29-2003 at 10:01 PM.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    thats basicly what i had, but with error checking.

  4. #4
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Only thing i can think of, is it opening the correct file? try not passing the file name, create a dummy and use that like this program.

  5. #5
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    What are you trying to pass, the file name? It seems you are passing the address of the array (im guessing its an array), but you're putting it into a single char pointer.
    Last edited by stumon; 03-29-2003 at 10:10 PM.

  6. #6
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    lol, sorry, lot o replies. try this.
    Code:
    extern int getfilesize(char *src[])

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    yeah, i am giving it a array. the array has a filename. the same thing works with functions like fopen, why wouldnt it work here?

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    extern int getfilesize(char *src[])

    why not just have the pointer and is a * and [] legal?

  9. #9
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    arsh, if its just an array, use this.
    Code:
    extern int getfilesize(char src[])
    sorry, my mistake.

  10. #10
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    what wrong with the pointer? fopen will use it, and it goes back to the same thing right?

  11. #11
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    it should, what does the array you are sending look like? whers that code?

  12. #12
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Here is an example off your code that works just fine. Hopefully it will help.
    Code:
    #include<stdio.h>
    #include<sys/stat.h>
    
    extern int blat(char *);
    
    int main()
    {
    	int booty;
    	char blah[] = {"c:/yourfilename.txt"};
    
    	booty = blat(blah);
    	printf("File size: %ld\n", booty);
    
    return 0;
    }
    
    extern int blat(char *blat)
    {
    	struct stat buf;
    	stat(blat,&buf);
    	printf("File size: %ld\n", buf.st_size );
    	return (buf.st_size);
    
    }
    Pardon the names, it was quick.

  13. #13
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    well it turns out i put in the wrong kind of slash!

    one more thing, what unit is st_mtime in?

    any one know of a cplusplus.com like reference site for this kind of stuff?

  14. #14
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    I recommend www.experts-exchange.com its pretty good.

    The structure stat contains at least the following members:

    type name description
    dev_t st_dev ID of device containing file
    ino_t st_ino file serial number
    mode_t st_mode mode of file (see below)
    nlink_t st_nlink number of links to the file
    uid_t st_uid user ID of file
    gid_t st_gid group ID of file
    dev_t st_rdev device ID (if file is character or block special)
    off_t st_size file size in bytes (if file is a regular file)
    time_t st_atime time of last access
    time_t st_mtime time of last data modification
    time_t st_ctime time of last status change
    blksize_t st_blksize a filesystem-specific preferred I/O block size for
    this object. In some filesystem types, this may
    vary from file to file
    blkcnt_t st_blocks number of blocks allocated for this object

  15. #15
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by mart_man00
    one more thing, what unit is st_mtime in?

    any one know of a cplusplus.com like reference site for this kind of stuff?
    http://www.rt.com/man/stat.2.html is a good man page site.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21