www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/07/30/01:01:02

From: Jamie Love <jamie DOT love AT clear DOT net DOT nz>
Newsgroups: comp.os.msdos.djgpp
Subject: more on file problems
Date: Thu, 30 Jul 1998 16:49:51 +1200
Organization: CLEAR Net, http://www.clear.net.nz/
Lines: 81
Message-ID: <35BFFB6F.A91B7F08@clear.net.nz>
NNTP-Posting-Host: d2-u53.wgtn.clear.net.nz
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019