I just tried out your method, and it leaves me scratching my head.
After stumbling briefly for lack of the header to define
back_inserter() and ostream_iterator() (thanks Google and SGI), the
code compiles fine:
This depends on the implementation. The standard does not specify
which header must be included by which; <iterator> probably got
__________code__________________
#include <fstream>
#include <vector>
#include <iterator>
using namespace std;
int main(){
const int DATACHUNK = 20;
char buffer[DATACHUNK];
ifstream filein("shifttest.cpp");
filein.read(buffer, DATACHUNK);
vector<int> filedata;
copy(buffer, buffer + DATACHUNK, back_inserter(filedata));
ofstream fileout("shifttest.joe");
copy(filedata.begin(), filedata.end(),
ostream_iterator<int>(fileout, "\n" ));
}
_____end code_________________
However, when I look at the file out, it contains:
35
105
110
99
108
117
100
101
32
60
105
111
115
116
114
101
97
109
62
10
which is the ASCII representation of the integer representation of the
ASCII sequence "#include <iostream>"
which, strangely enough, happens to be the first line of
"shifttest.cpp" ;-)
You asked for binary, that is what I gave you. If you want the ASCII
This is really not at all what I am wanting to do. Now my 20 bytes is
represented by 93 bytes
93 ?? Why do you say that?
of a rather odd data-type...neither characters
nor integers, but rather some strange beast that combines the worst of
both worlds.
These numbers you saw are the ASCII value of the characters in the
file. The thing is, characters and integers are actually the very
same thing, it's just the output which makes the difference : ints are
displayed as numbers and chars are displayed as characters, which
depend on your implementation (but you are probably using ASCII).
Remember your subject is "Binary file I/O", not "Text file I/O".
I'm left wondering, in this strange new world of C++ do I need to get
used to dealing with ASCII representations of numbers for file I/O?
It depends on what you want. In the case of a simple text file
(remember, *text* is a ambiguous term in programming, everything boils
down to zeros and ones) , values would be ASCII numbers and text would
be the representation on the screen (65 would be 'A').
In the case of a binary file (such as an image), values would be
simple numbers formatted according to the image's type (jpg, bmp..)
and text would be... garbage, since these numbers would be printed
according to the ASCII table (remember when you first started and
tried to display binary files on screen? Loads of smileys and beeps
and ascii graphics..).
However, this seems a bit tedious, considering that this rigamarole
doesn't really do anything to the internal data. I feel like there's
something really basic that I don't *get* about streams... All I
really want to do is "get at" the data in a file and treat that data
as numbers typed to the native processor word size...then, manipulate
the data and write the data out to a second file. Consider, for
example, that the file consists of a binary bitmap and I want to
invert it, or rotate it or something.
In that case, you would store every byte in a vector of whatever
(unsigned char would be the best, I think), you skip the header until
the data, you invert it and store the whole thing in a new file.
The actual type of the vector (or array, as you wish) does not matter
except for the memory wasted.
Anyway...It's apparent that I have a lot to learn. This C++ is
tantalizing me...the code is about 10 to 20 x faster than my old
16-bit compiler...but jeez...what would seem to be a simple
manipulation can become so frustrating!!! It feels a little like
typing with my toes.
Hehe.. and you're still only playing with i/o.
Jonathan