home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / gnu / g / help / 1403 < prev    next >
Encoding:
Text File  |  1992-11-06  |  2.5 KB  |  70 lines

  1. Newsgroups: gnu.g++.help
  2. Path: 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
  3. From: kelly@spyro.cray.com (Matt Kelly)
  4. Subject: fstream problem???
  5. Message-ID: <1992Nov5.134127.9894@walter.cray.com>
  6. Lines: 59
  7. Sender: kelly@spyro (Matt Kelly)
  8. Nntp-Posting-Host: spyro.cray.com
  9. Date: 5 Nov 92 13:41:26 CST
  10.  
  11.  
  12. I am using gcc2.2.2 to compile a program which opens a file for reading and
  13. writing.  I first read in the entire file, then write the read in data back out
  14. to the same file in a different order.  I use this code sequence:
  15.  
  16. fstream fout;
  17. union {                         
  18.     unsigned long lb[2208];     
  19.     unsigned long ls[23][96];  
  20. };
  21.  
  22.  
  23. fout.open("filex",(ios::in | ios::out));              //open file for r/w
  24. if (fout.fail()) {
  25.     cout << "File: " << "filex" << " didn't open\n"
  26.          << flush;
  27.     return(-1);
  28. }
  29. fout.read((char *)lb,(2208*sizeof(long)));           //read entire file in
  30. for (int i = 0; i < 9; i++) {
  31.    fout.seekp((i*96*sizeof(long)),ios::beg);        //seek to new location in file
  32.    fout.write((char *)ls[(asics-1)-i],(sizeof(long)*96)); //write to new loc
  33.    if (fout.fail()) {
  34.        cout << "Write failed to: filex\n" << flush;
  35.        return(-1);
  36.    }
  37. }
  38.  
  39.  
  40. The problem I am seeing is that the write I do inside the "for" loop fails
  41. from the start!  
  42.  
  43. I have isolated the problem to this: if the file that I opened for reading/
  44. writing is smaller than the number of bytes I am trying to READ from it (with
  45. the read statement just befor the "for" loop), then the write function will
  46. fail, and fout.fail() will return 2 (which is correct if it really failed).
  47. If I reduce the number of bytes read in the fout.read() statement to a number
  48. which is <= the number of bytes in the file, the fout.write() works.
  49.  
  50. In C, if you do a read() asking for more bytes than are available in a file,
  51. it simply reads as many as it can and puts them in the buffer you pass it.
  52. This doesn't seem to be the case here.
  53.  
  54. Is this a bug, or is this how it's supposed to work?  I couldn't find anything
  55. about this in any of my C++ references.
  56.  
  57. Thanks in advance,
  58.  
  59. Matt K.
  60.  
  61. ==============================================================================
  62. kelly@els.cray.com                   |
  63.                                      | Semper ubi sub ubi.
  64. Opinions expressed herein are just   |
  65. that.  Mine, not Cray's.             | -Julius Caesar
  66.                                      |
  67. ==============================================================================
  68.  
  69.  
  70.