Thread: Get file size of file that contains EOF

  1. #1
    Chad Johnson
    Join Date
    May 2004
    Posts
    154

    Question Get file size of file that contains EOF

    How can I get the file size of a file if the file contains EOF? I tried
    Code:
        in_file.open(fname.c_str(), ios::binary);
    
        if (in_file == NULL)
            return -1;
    
        in_file.seekg(0, ios::end);
        fsize = in_file.tellg();
        in_file.seekg(0, ios::beg);
        
        tmp_buffer = new char[fsize];
        
        in_file.get(tmp_buffer, fsize, '\0');
        in_file.close();
    but it's stopping at the first EOF (0 byte) it finds.

  2. #2
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Try this:

    Code:
    std::fstream iFile(filename.c_str(), std::ios::ate); // ios::ate == At the end of the file.
    				int size = iFile.tellg(); // Get the size of the file.
    				iFile.seekg(0, std::ios::beg); // Seek to the beginning of the file.

  3. #3
    Chad Johnson
    Join Date
    May 2004
    Posts
    154
    No luck

    EDIT: Actually, it got the file size correctly. So, that worked. Let me play around with the rest of the code and I'll post my results.
    Last edited by ChadJohnson; 03-12-2005 at 09:04 PM.

  4. #4
    Chad Johnson
    Join Date
    May 2004
    Posts
    154
    Well, I got the file size, but any attempt to input the entire file using ifstream::get or fread don't work. They apparently stop at the first 0 byte.

  5. #5
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Do you use "ios::in | ios::ate" as your input flags, or just ate? If you don't have ios::in and use fstream (not ifstream), then you won't be able to read().

    Also, use tellg() to see if you're at byte 0. If not, you need to seek there.

    Can I see your code? Or at least the pertinant parts?



    Edit: http://www.cplusplus.com/doc/tutorial/tut6-1.html

    See that link for more help with file I/O. It's very nice.
    Last edited by Lithorien; 03-12-2005 at 10:39 PM.

  6. #6
    Chad Johnson
    Join Date
    May 2004
    Posts
    154
    It does input and print the first character, which is non-zero, then it stops. The second character is a 0-byte;

    Code:
        fstream in_file;
        char *tmp_buffer;
        
        in_file.open(fname.c_str(), ios::in|ios::ate);
    
        if (in_file == NULL)
            return -1;
    
        // get size of file
        fsize = in_file.tellg();
        in_file.seekg(0, ios::beg);
        
        tmp_buffer = new char[fsize];
        
        // read file into buffer
        in_file.read(tmp_buffer, fsize);
        in_file.close();
    	
    	cout << tmp_buffer;

  7. #7
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    The reason that you might not be seeing the whole output is your cout << statement.

    cout stops when it reaches a \0 or 'null' character. A 0-byte character from a binary file will translate to NULL when you read it in. So you probally want another way to display your data.. a for loop might do what you want, or a while loop (even better).

    I believe this is your problem, anyway..

  8. #8
    Chad Johnson
    Join Date
    May 2004
    Posts
    154
    That was the problem. Thanks a lot! I think I'll be using C from now on instead of the STL to handle stuff with memory, and C++ for other stuff.

  9. #9
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    Don't cout << or loop it.

    Use cout's member function write().

  10. #10
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by Kybo_Ren
    Don't cout << or loop it.

    Use cout's member function write().
    Even better than a loop. Nice catch.

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>but it's stopping at the first EOF (0 byte) it finds.
    Actually, there is no such thing as an EOF byte. What you're talking about is a NULL byte

    >>I think I'll be using C from now on instead of the STL to handle stuff with memory
    Why? If you use STL, you don't even have to delete your memory.
    Code:
    ifstream file(fname.c_str());
    file.seekg(0, ios::end);
    streampos fileSize = file.tellg();
    
    file.seekg(0, ios::beg);
    vector<char> data(fileSize);
    file.read(&(data[0]), fileSize);
    
    ...
    
    cout.write(&(data[0]), fileSize);
    Anyway, pretty much anything written in C is valid C++ too.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  12. #12
    Chad Johnson
    Join Date
    May 2004
    Posts
    154

    Question Example

    Here's a good example why. Maybe (hopefully) you can give me a solution.

    The file is now cached in memory in a variable of type std::string. The first two bytes in hex are 12 00, which give the length of a substring within the buffer following these two bytes.

    How can I copy the value of these two bytes to an integer variable directly from the buffer (NOT from the original file), giving it the value 18?
    Last edited by ChadJohnson; 03-13-2005 at 04:51 PM.

  13. #13
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Well, first of all (as far as I can tell) your string will only contain these first two bytes - since the second byte is 0x00, i.e. 0 or NULL, the string is terminated after the first character. You should use a vector of char or unsigned char if you're planning on handling binary data, to avoid this sort of conflict.

    Anyway, assuming these 2 bytes represent a short (or unsigned short):
    Code:
    //Assuming the data's stored in a vector<char>:
    unsigned short substrLen = *(unsigned short*)(&(data[0]));
    This basically reinterprets the address of the first byte as a pointer to unsigned short, then dereferences it to retrieve the value of the first (sizeof(unsigned short)) bytes, interpreted as an unsigned short.

    You could also use reinterpret_cast<unsigned short*> or whatever instead of just (unsigned short*) for the typecast, but I'm not too familiar with exactly which cast does what, so I usually just stick with good 'ol C-style typecasting.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

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. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. Replies: 11
    Last Post: 03-25-2003, 05:13 PM