how to know how much bytes does this file contain?

This is a discussion on how to know how much bytes does this file contain? within the C++ Programming forums, part of the General Programming Boards category; hiya., okey i have this, ifstream fin("data.dat"); but i'd don't have any idea how big this is so how am ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    142

    how to know how much bytes does this file contain?

    hiya.,

    okey i have this,

    ifstream fin("data.dat");

    but i'd don't have any idea how big this is so how am i goign to use,

    fin.read(...) since ifstream::read(...) wants the size of the bytes to read? same problem with the array size, i have to know much should i create. is there a way to know how big the file is?

    many thanks

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    I think if you're going to use read(), you would be reading in a class or a structure of some sort, and the size would be the size of the class/or structure. And the file would contain several of these classes, and you would read them in via a loop, and when you have one instance of a class in memory, you would store it into some kind of container. I would use the STL personally...

    On the other hand, if the file is just text, you should probably be using getline() instead, and you could use the same approach of reading a line at a time and storing each line in some kind of container.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Yep. You can use this method.
    Code:
    #include <sys/stat.h>
      struct stat results;
      if (stat("input.bin", &results) == 0)
        // The size of the file in bytes is in
        // results.st_size
      else
        // An error occurred
    or... ( fragment )

    Code:
      int l, m;
    
      l = file.tellg();
      file.seekg (0, ios::end);
      m = file.tellg();
      cout << "size of " << filename;
      cout << " is " << (m-l) << " bytes.\n";
    
      file.close( );
    tellg tells you were the file pointer is currently at. Seekg seeks a number of bytes OR an offset. So I seek to the end and ask where the pointer is at. Pretty simple.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  4. #4
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    Are you not allowed to use the sizeof() command??
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    32
    Originally posted by OneStiffRod
    Are you not allowed to use the sizeof() command??
    sizeof () tells how you the byte size of a data type.

    MrWizard posted what is needed.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    thanks guys!

    mrwizard: hiya, well that's what i'm thinking too but am guessing there's a functino for this, but still thanks very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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