delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/02/06/20:08:35

Message-ID: <36BCDEF1.CCB01E25@idirect.com>
Date: Sat, 06 Feb 1999 19:31:45 -0500
From: Martin Smith <mksmith AT idirect DOT com>
X-Mailer: Mozilla 4.5 [en] (Win95; I)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: Re: djgpp: c++ and binary files
References: <36bc9292 DOT 14696703 AT news DOT dlc DOT fi>
NNTP-Posting-Host: ts7-70t-28.idirect.com
X-Trace: 6 Feb 1999 19:31:23 +0500, ts7-70t-28.idirect.com
Organization: via Internet Direct
Lines: 86
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

See the lines after the comments.



Collonello wrote:

> Okay, I've a problem.
> Somebody tell me what's wrong in this program:
>
> ---------
> #include <fstream.h>
>
> class Foo
> {
> public:
>   Foo(int);
>   ~Foo();
>
>   void setX(int);
>   int  getX() const;
>
> private:
>   int x;
> };
>
> Foo::Foo(int set_x)
> :
>   x(set_x)
> {
> }
>
> void Foo::setX(int set_x)
> {
>   x = set_x;
> }
>
> int Foo::getX() const
> {
>   return x;
> }
>
> Foo::~Foo() {}
>
> int main()
> {
>   Foo *foo = new Foo(0);
> // You probably need to specify "output mode"
> // YOUR CODE  ofstream fout("foo.dat", ios::binary);
>   ofstream fout("foo.dat", ios::out | ios::binary);  // my suggestion
>   fout.write(foo, sizeof(foo));
>   if(!fout)
>   {
>     cout << "Doh... Couldn't write foo.dat" << endl;
>     return -1;
>   }
>   fout.close();
>
>   cout << foo->getX() << endl;
>   foo->setX(3);
>   cout << foo->getX() << endl;
>   // again
>   // YOUR CODE ifstream fin("foo.dat", ios::binary);
>   ifstream fin("foo.dat", ios::in | ios::binary);  // my suggestion
>   fin.read(foo, sizeof(foo));
>   if(!fin)
>   {
>     cout << "Doh... Couldn't read foo.dat. Aborted. " << endl;
>     return -1;
>   }
>   fin.close();
>
>   cout << foo->getX() << endl;
>
>   return 0;
> }
> ----------
>
> For some reason the program compiled with djgpp
> can't write the binary file and Borland 4.52 can.
>
> Sorry, tired of explaining. Just check source code and tell me
> what's the darned thing that messes with me.
>
> Hate this,
> Tom

- Raw text -


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