Jonathan,
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:
__________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" ;-)
This is really not at all what I am wanting to do. Now my 20 bytes is
represented by 93 bytes of a rather odd data-type...neither characters
nor integers, but rather some strange beast that combines the worst of
both worlds.
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?
Or do I need to always break my 4-byte integers into individual bytes
prior to I/O if I don't want to waste storage space? I suppose this
would be pretty easy...something like:
//not tested
int bytetowrite;
char holdword[4];
for(int i = 0; i < 4; i++)
holdword[i] = (bytetowrite & (255 << (i * 8))) >> (i * 8);
//holdword now contains, small-byte first, the data from bytetowrite
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.
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.
Thanks for the help people. It is beginning to make some sense.
Joe
Jonathan Mcdougall <DELjonathanmcdougall@yahoo.ca> wrote in message news:<kbm1ivk0hojcea81ik83bsb54dk1qn1n31@4ax.com>. ..[color=blue]
> On Thu, 24 Jul 2003 20:39:23 -0400, Jonathan Mcdougall
> <DELjonathanmcdougall@yahoo.ca> wrote:
>[color=green]
> >On 24 Jul 2003 16:56:53 -0700,
mango_maniac@yahoo.com (J. Campbell)
> >wrote:
> >[color=darkred]
> >>OK...I'm in the process of learning C++. In my old (non-portable)
> >>programming days, I made use of binary files a lot...not worrying
> >>about endian issues. I'm starting to understand why C++ makes it
> >>difficult to read/write an integer directly as a bit-stream to a file.
> >> However, I'm at a bit of a loss for how to do the following. So as
> >>not to obfuscate the issue, I won't show what I've been attempting ;-)
> >>
> >>What I want to do is the following, using the standare IO streams.[/color]
> >
> ># include <fstream>
> ># include <vector>
> ># include <algorithm>[/color]
>
> Forget these ones :
>[color=green]
> ># include <sstream>
> ># include <iostream>
> ># include <string>
> >
> >[color=darkred]
> >>1) open an arbitrary file (file1).[/color]
> >
> >std::ifstream file1("f.txt");
> >[color=darkred]
> >>2) starting with the first byte in (file1), read a chunk of data into
> >>an array of integers.[/color]
> >
> >const int CHUNK = 128;
> >
> >char buffer[CHUNK];
> >file1.read(buffer, CHUNK);
> >
> >std::vector<int> data;
> >std::copy(buffer, buffer + 128, std::back_inserter(data));[/color]
>
> std::copy(buffer, buffer + CHUNK, std::back_inserter(data));
>[color=green]
> >[color=darkred]
> >>3) manipulate the array, as integer data,[/color]
> >
> >void manipulate(std::vector<int> &v);
> >
> >
> >manipulate(data);
> >[color=darkred]
> >>and then output the contents
> >>of the array to another file (file2).[/color]
> >
> >std::ofstream file2("g.txt");;
> >std::copy(data.begin(), data.end(),
> > std::ostream_iterator<int>(std::cout, "\n"));[/color]
>
> std::copy(data.begin(), data.end(),
> std::ostream_iterator<int>(file2, "\n"));
>
>
> Sorry about that,
>
> Jonathan[/color]