-
ifstream and nulls
Sorry, this is probably a basic question but I've always used MFC for I/O. I'm using an ifstream and it stops reading as soon as it hits a null. I'm trying to read the end of a file. How do I get it to read everything and not stop at the null. Here's my code. Thanks
Code:
std::ifstream ifs;
ifs.open(m_FilePath, std::ifstream::in | std::ifstream::binary);
ifs.seekg(-2048l, ifs.end);
char buf[2048];
ifs.read(buf, 2048l);
-
Re: ifstream and nulls
buf is sized at 2048, but 20481 chars are tried to be read??
-
Re: ifstream and nulls
That's a letter l, not a 1. It works for files without a null, but stops where there's a null.
-
Re: ifstream and nulls
Turns out because I'm reading into a char array, the debugger isn't showing anything after the null. The data is there though
-
Re: ifstream and nulls
You could read it into a std::string as std::string can contain nulls. Something like:
Code:
std::ifstream ifs;
std::string buf(2048, 0);
ifs.open(m_FilePath, std::ifstream::in | std::ifstream::binary);
ifs.seekg(-2048, ifs.end);
ifs.read(buf.data(), 2048);