Thread View: gnu.g++.help
1 messages
1 total messages
Started by Matt Kelly
Thu, 05 Nov 1992 19:41
fstream problem???
Author: Matt Kelly
Date: Thu, 05 Nov 1992 19:41
Date: Thu, 05 Nov 1992 19:41
57 lines
2630 bytes
2630 bytes
I am using gcc2.2.2 to compile a program which opens a file for reading and
writing. I first read in the entire file, then write the read in data back out
to the same file in a different order. I use this code sequence:
fstream fout;
union {
unsigned long lb[2208];
unsigned long ls[23][96];
};
fout.open("filex",(ios::in | ios::out)); //open file for r/w
if (fout.fail()) {
cout << "File: " << "filex" << " didn't open\n"
<< flush;
return(-1);
}
fout.read((char *)lb,(2208*sizeof(long))); //read entire file in
for (int i = 0; i < 9; i++) {
fout.seekp((i*96*sizeof(long)),ios::beg); //seek to new location in file
fout.write((char *)ls[(asics-1)-i],(sizeof(long)*96)); //write to new loc
if (fout.fail()) {
cout << "Write failed to: filex\n" << flush;
return(-1);
}
}
The problem I am seeing is that the write I do inside the "for" loop fails
from the start!
I have isolated the problem to this: if the file that I opened for reading/
writing is smaller than the number of bytes I am trying to READ from it (with
the read statement just befor the "for" loop), then the write function will
fail, and fout.fail() will return 2 (which is correct if it really failed).
If I reduce the number of bytes read in the fout.read() statement to a number
which is <= the number of bytes in the file, the fout.write() works.
In C, if you do a read() asking for more bytes than are available in a file,
it simply reads as many as it can and puts them in the buffer you pass it.
This doesn't seem to be the case here.
Is this a bug, or is this how it's supposed to work? I couldn't find anything
about this in any of my C++ references.
Thanks in advance,
Matt K.
==============================================================================
kelly@els.cray.com |
| Semper ubi sub ubi.
Opinions expressed herein are just |
that. Mine, not Cray's. | -Julius Caesar
|
==============================================================================
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads