Thread: Get file size when in buffer

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    16

    Get file size when in buffer

    I gota BYTE* buffer, which contains the contents of a file.

    Problem is when I want to output the contents of the buffer, into a new file (copy).
    I need to retrieve the size of the BYTE* buffer, and casting it to char* and using strlen() doesn't work since it breaks on EOF-bytes, which there sometimes are in files.

    Any solutions?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    None. You need to know the size beforehand.
    Although if it's a text file, then strlen might work to get the length of the text inside the file.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Consider using std::vector<BYTE> instead of BYTE* as it contains more features (like buffer size and automatic memory deallocation).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Magos View Post
    Consider using std::vector<BYTE> instead of BYTE* as it contains more features (like buffer size and automatic memory deallocation).
    and I'm pretty sure vector doesn't care about NULLs or EOF bytes, the size it reports is simply the size of the data you put in it.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, I agree with that. The problem, however, with using vector<BYTE> and file-reading functions is that you either need to use another buffer to store temporary results, or read one byte at a time. Reading one byte at a time is inefficient, but would technically work. Reading multiple bytes still presents the problem of knowing how many byte was read, which is what the original post (sort of) is asking for.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Quote Originally Posted by cpjust View Post
    and I'm pretty sure vector doesn't care about NULLs or EOF bytes, the size it reports is simply the size of the data you put in it.
    If that size isn't what you need, then what size do you need?
    A vector of size 0 is equal to a NULL pointer.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by matsp View Post
    Yes, I agree with that. The problem, however, with using vector<BYTE> and file-reading functions is that you either need to use another buffer to store temporary results, or read one byte at a time.
    I'm not sure the impact would be that great, since C++ iostreams are buffered. It would be easy to do some time trials to be sure. By default, I wouldn't worry about it. Maybe something like this:

    Code:
    std::ifstream input_file(...);
    input_file >> std::noskipws;
    std::vector<BYTE> file_data(std::istream_iterator<char>(input_file), std::istream_iterator<char>());

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by matsp View Post
    Yes, I agree with that. The problem, however, with using vector<BYTE> and file-reading functions is that you either need to use another buffer to store temporary results, or read one byte at a time. Reading one byte at a time is inefficient, but would technically work. Reading multiple bytes still presents the problem of knowing how many byte was read, which is what the original post (sort of) is asking for.

    --
    Mats
    What is wrong with
    Code:
    std::stringstream ss;
    // this copies the entire contents of the file into the string stream
    ss << fin.rdbuf();
    // get the string out of the string stream
    std::string contents = ss.str();
    // construct the vector from the string.
    std::vector<char> buff(contents.begin(), contents.end());
    ??
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    What is wrogn with keepign track of how many bytes you read form th source file, you need to do this anyway, sicne a read request for X bytes is not guaranteed to return X bytes even if there are more than X bytes left in the file.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Would this be more efficient than vart's example?
    Code:
    std::vector<char> buff;
    std::copy(std::istreambuf_iterator<char>(fin),
              std::istreambuf_iterator<char>(),
              std::back_inserter(buff));
    rdbuf() is very efficient, but there's a temporary string involved.
    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

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you look back at a thead I posted in a few months ago, this method was significantly faster:
    Code:
    void Test4( ifstream&  file )
    {
    	char block[BUFSIZ + 1];
    	string fileData;
    	while ( file.read( block, BUFSIZ ) )
    	{
    		fileData += block;
    	}
    	cout << "Test4 Bytes Read: " << fileData.size() << endl;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. buffer contents swapping
    By daluu in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2004, 02:34 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM