How do you get how many bytes were read with the ifstream::read function?
Tell is saying the file is 10 bytes and windows says it is 10 bytes too but there are only 8 bytes in the file so when I read it, it is only reading the 8 bytes so I end up with too large of a buffer.
The read() function reads data previously written to a file. If any portion of a regular file prior to the end-of-file has not been written, read() shall return bytes with value 0. For example, lseek() allows the file offset to be set beyond the end of existing data in the file.
File Handling in C++ To read a character sequence from a text file, we'll need to perform the following steps: Create a stream object. Connect it to a file on disk. Read the file's contents into our stream object.
ifstream is an input file stream. It is a special kind of an istream that reads in data from a data file. ofstream is an output file stream. It is a special kind of ostream that writes data out to a data file.
You can find out by calling gcount()
on a stream immediately after you read.
ifs.read(buf, sizeof buf); std::streamsize bytes = ifs.gcount();
There is a function called readsome(...)
that does what you want:
streamsize readsome ( char* s, streamsize n );
Return Value The number of characters extracted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With