Thread: file size detection

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Okay, so what is the output from the program I posted?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    Dec 2011
    Location
    Namib desert
    Posts
    94
    Isn't it easier to determine the file size before opening it ?
    For example;
    Code:
    #include <sys/stat.h>
    #include <stdio.h>
    
    int main()
    {
      struct stat thisInode;
    
      if ( (stat("/home/dev_x/somefile.zip", &thisInode)) == 0)
        printf("File size in bytes is: %ld\n", thisInode.st_size);
      else
        printf("Could not read file attributes\n");
    
      return(0);
    }

  3. #18
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by ddutch View Post
    Isn't it easier to determine the file size before opening it ?
    For example;
    Code:
    #include <sys/stat.h>
    #include <stdio.h>
    
    int main()
    {
      struct stat thisInode;
    
      if ( (stat("/home/dev_x/somefile.zip", &thisInode)) == 0)
        printf("File size in bytes is: %ld\n", thisInode.st_size);
      else
        printf("Could not read file attributes\n");
    
      return(0);
    }
    stat() is Posix, which will certainly work in the unix-like environment being discussed here, but as Elysia was kind enough to point out in another thread, Posix is no longer supported by the latest versions of windows, so this code is not portable. Opening the file read-only and seeking to the end is the portable way, if portability is important.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2012, 10:35 AM
  2. Replies: 8
    Last Post: 01-30-2011, 02:55 PM
  3. Replies: 3
    Last Post: 08-13-2006, 05:58 AM
  4. File detection
    By lavaman in forum C++ Programming
    Replies: 4
    Last Post: 11-15-2002, 07:02 PM
  5. File Size and File Size on Disk
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 12-15-2001, 08:03 PM