Mail Archives: djgpp/1998/07/30/01:01:02
Well, because this problem is really annoying, I thought I would create
a basic program to test my file handling. Coding the file was ok, with
no warnings on compilation (command line: gxx -Wall -o tst.exe tst.cc)
but when I run it, I comes up with error writing to file on the first
integer. The question is, does anyone know why? The file (test.dat) is
created with length 0 also. Also, if I have mucked up my code then
please tell me as as far as I can make out, this should work!
I use:
bnu281
djdev201
gpp281/lgp281
lgpp280
Also, could you please cc this to my email, if possible, thanks
Any ideas would be of use. For the moment, I am just going to ignore the
problem and code my derived classes (from the linked list class that
this problem comes from)
The code follows
#include <fstream.h>
#include <iostream.h>
int writeInts(ostream &o);
int readInts(istream &in);
int main() {
ofstream infle("test.dat", ios::bin | ios::trunc);
ifstream outfle;
if(!writeInts(infle)) {
cout << "\nError writing to file";
return 0;
}
infle.close();
outfle.open("test.dat", ios::bin);
if(!readInts(outfle)) {
cout << "\nError reading to file";
return 0;
}
return 1;
}
int writeInts(ostream &o) {
for(int i = 0; i < 10; i++) {
cout << i << endl;
o.write((char *)&i, sizeof(int));
if(!o.good()) {
return 0;
}
}
return 1;
}
int readInts(istream &in) {
int c = -1;
for(int i = 0; i < 10; i++) {
in.read((char *)&c, sizeof(int));
cout << c << endl;
if(!in.good()) {
return 0;
}
}
return 1;
}
--
Jamie Love
- jamie DOT love AT clear DOT net DOT nz
- http://www.geocities.com/siliconvalley/horizon/4532
----
- Raw text -