-
December 8th, 2021, 04:27 PM
#1
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);
-
December 9th, 2021, 04:20 AM
#2
Re: ifstream and nulls
buf is sized at 2048, but 20481 chars are tried to be read??
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 9th, 2021, 08:19 AM
#3
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.
-
December 9th, 2021, 08:53 AM
#4
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
-
December 9th, 2021, 09:02 AM
#5
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);
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|