Thread: file length

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    244

    file length

    how safe is it to retrieve the size of a file by doing this:
    Code:
    fstream fs;
    ..
    fs.open("some_file", std::ios::int | std::ios::binary);
    ...
    fs.seekg(0, std::ios_base::end);
    unsigned size = fs.tellg();
    also, how fast is it?

    also, what would be the best way platform-independent way to get the size of a file?
    (i want the size of a file because i want to store the content of it in memory - and want to allocate the buffer only one)
    signature under construction

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    You don't need to create a buffer for this, an std::string will do just fine, especially if you read line by line.
    Code:
    while(std::getline(myFile, myString, '\n'))
    {
    
    }

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    ah true... std::getline! thx
    signature under construction

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    hm ist it possible to use a special delimiter token so it would just read till the end of the stream?
    signature under construction

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    uh oops, i just realized i need to read binary files. ive never used strings to store binary data in them.
    does that even work? (also, i can't really use getline then - since it discards all newline characers)
    signature under construction

  6. #6
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    If the file contains content that your own program wrote, Why don't you make the first two(or four) bytes be an int which represents the size of the file excluding the two bytes(or four). I use this teqnique to when I save RAW files making sure that the first 4+4 bytes contain the image width and height.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    but thats so redundant
    i really wonder if seekg'int to the end with ios::end and then tellg'ing the position is guaranteed to work on fstreams. well i have just got my project to compile but its already very late. i guess ill test if it even works on my machine after i get up again.
    signature under construction

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Well... if the file size is bigger than an int... kabum!

    A top notch solution is boost::filesystem.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    ok, at least it works on my machine - which still doesnt mean that piece of code works on other machines.
    (i've never needed the seek direction argument of seekg before - so i was just guessing that it might work)
    signature under construction

  10. #10
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    >> boost::filesystem

    hm! that is really an option. yeah, ill use that eventually.
    signature under construction

  11. #11
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I remember reading some about it before. I believe in codeproject.com. You may want to check there.

    The lasting impression I got from it was that there were always tradeoffs. But I think your solution is indeed portable. I can't see a reason why not since tellg is standard. The only problem I see is indeed the file being bigger than what an int can carry (or the inherently non portable problem of relying on the arithmetic types)
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The only problem I see is indeed the file being bigger than what an int can carry
    But then the file would have to be gigabytes in size?! Could you even create a buffer that big? Would it be practical?

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I can't think of too many problems which can't be solved by reading in part of the file, dealing with it in some way, then re-using the buffer for the next part of the file.

    You don't load an entire movie into memory just to play it for example.

    Even for editing a movie, you only need to read in the frames necessary for the immediate task. It might be worth scanning the whole file in advance to build an index of where all the useful information is, but this too doesn't need the whole file in memory at once.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    lol, of course i'm not using that code to load full movies and stuff.
    currently i just use it in a helper function that creates a open gl shader from a file.
    ok, thats still text - so getline could have been used. but now its more general and e.g. i can use it to load byte code from script files.

    of course ill rewrite the code later to use (de)serialization techniques. but i just wanted a quick helper function for now.
    signature under construction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  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