user230821
user230821

Reputation: 1123

ifstream, bytes read?

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.

Upvotes: 32

Views: 31536

Answers (2)

Khaled Alshaya
Khaled Alshaya

Reputation: 96859

There is a function called readsome(...) that does what you want:

streamsize readsome ( char* s, streamsize n );

Return Value The number of characters extracted.

Upvotes: 4

Alex B
Alex B

Reputation: 84822

You can find out by calling gcount() on a stream immediately after you read.

ifs.read(buf, sizeof buf);
std::streamsize bytes = ifs.gcount();

Upvotes: 59

Related Questions