S
Steven T. Hatton
I'm still not completely sure what's going on with C++ I/O regarding the
extractors and inserters. The following document seems a bit inconsistent:
http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#1
Copying a file:
WRONG WAY:
#include <fstream>
std::ifstream IN ("input_file");
std:fstream OUT ("output_file");
OUT << IN; // undefined behavior
RIGHT WAY:
//[T]he easiest way to copy the file is:
OUT << IN.rdbuf();
HOWEVER:
"First, ios::binary has exactly one defined effect, no more and no less.
Normal text mode has to be concerned with the newline characters, and the
runtime system will translate between (for example) '\n' and the
appropriate end-of-line sequence (LF on Unix, CRLF on DOS, CR on Macintosh,
etc)....
Second, using << to write and >> to read isn't going to work with the
standard file stream classes, even if you use skipws during reading. Why
not? Because ifstream and ofstream exist for the purpose of formatting, not
reading and writing. Their job is to interpret the data into text
characters, and that's exactly what you don't want to happen during binary
I/O.
BUT IT SAID:
[T]he easiest way to copy the file is:
OUT << IN.rdbuf();
Does that only apply to "text" files?
"Third, using the get() and put()/write() member functions still aren't
guaranteed to help you. These are "unformatted" I/O functions, but still
character-based. (This may or may not be what you want, see below.)"
I saw below, but don't know what I was supposed to see. Is it the endian
stuff?
If I open a file in binary mode, then f.rdbuf() >> stringstrm, is the entire
file going to be faithfully represented bit-for-bit in the
std::stringstream? If not, how will it have been changed? Note that I
made no mention of unsetting skipws here.
You may think I'm just had-headed, and can't understand that I shouldn't use
the overloaded shift operators for unformatted data. Well, suppose someone
else were to do that, and it worked for them. Is there a potential that it
could cause problems for me? I certainly have used the above method to
read "raw" data in the past.
Also, I _do_ want to "format" the data. I want to parse and ELF file into
its elementary components.
extractors and inserters. The following document seems a bit inconsistent:
http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#1
Copying a file:
WRONG WAY:
#include <fstream>
std::ifstream IN ("input_file");
std:fstream OUT ("output_file");
OUT << IN; // undefined behavior
RIGHT WAY:
//[T]he easiest way to copy the file is:
OUT << IN.rdbuf();
HOWEVER:
"First, ios::binary has exactly one defined effect, no more and no less.
Normal text mode has to be concerned with the newline characters, and the
runtime system will translate between (for example) '\n' and the
appropriate end-of-line sequence (LF on Unix, CRLF on DOS, CR on Macintosh,
etc)....
Second, using << to write and >> to read isn't going to work with the
standard file stream classes, even if you use skipws during reading. Why
not? Because ifstream and ofstream exist for the purpose of formatting, not
reading and writing. Their job is to interpret the data into text
characters, and that's exactly what you don't want to happen during binary
I/O.
BUT IT SAID:
[T]he easiest way to copy the file is:
OUT << IN.rdbuf();
Does that only apply to "text" files?
"Third, using the get() and put()/write() member functions still aren't
guaranteed to help you. These are "unformatted" I/O functions, but still
character-based. (This may or may not be what you want, see below.)"
I saw below, but don't know what I was supposed to see. Is it the endian
stuff?
If I open a file in binary mode, then f.rdbuf() >> stringstrm, is the entire
file going to be faithfully represented bit-for-bit in the
std::stringstream? If not, how will it have been changed? Note that I
made no mention of unsetting skipws here.
You may think I'm just had-headed, and can't understand that I shouldn't use
the overloaded shift operators for unformatted data. Well, suppose someone
else were to do that, and it worked for them. Is there a potential that it
could cause problems for me? I certainly have used the above method to
read "raw" data in the past.
Also, I _do_ want to "format" the data. I want to parse and ELF file into
its elementary components.