🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.g++.help
Article #440

fstream problem???

#440
From: Matt Kelly
Date: Thu, 05 Nov 1992 19:41
57 lines
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
                                     |
==============================================================================
Technical Details
Message-ID: <1992Nov5.134127.9894@walter.cray.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!stanford.edu!agate!netsys!ukma!cs.widener.edu!umn.edu!mmm.serc.3m.com!mmc.mmmg.com!timbuk.cray.com!walter.cray.com!spyro!kelly

Show Full Headers
Complete Article Headers
Newsgroups: gnu.g++.help
Path: arc1-ca.pugleaf.net!sparky!uunet!stanford.edu!agate!netsys!ukma!cs.widener.edu!umn.edu!mmm.serc.3m.com!mmc.mmmg.com!timbuk.cray.com!walter.cray.com!spyro!kelly
From: kelly@spyro.cray.com (Matt Kelly)
Subject: fstream problem???
Message-ID: <1992Nov5.134127.9894@walter.cray.com>
Lines: 59
Sender: kelly@spyro (Matt Kelly)
Nntp-Posting-Host: spyro.cray.com
Date: 5 Nov 92 13:41:26 CST