GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-Aug-2008, 10:32
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 582
Howard_L has a spectacular aura aboutHoward_L has a spectacular aura about

using fstream::fstream , question about error status


Hi,,, I have been fooling around with using the fstream class to read AND write to a file.
I was having a problem writing the the file a second time after a previous write and readback.
I searched and learned about checking error status and tracked down the problem for which I found two solutions. whoop...
But the two solutions leave me with the question of which is the better or more proper to get in the habit of using:
CPP / C++ / C Code:
//if I do a regular old : 
  while(( c = fh.get()) != EOF)
    cout.put(c);
//..it leaves fh with the 'eof' and 'fail' bits set....  I can fix that with:
  fh.clear();

//whch works ok , ...or I could find the current size of the file and use something like: 
   for(i = 0; i < size; i++) 
  {  c = fh.get();
     cout.put(c);   ...

// with no clear() which 'works just as well (?)'  I guess that's my question... 
// Which of those options would be the 'proper' way to go?
// I don't like seeng an 'error code' but going the extra step of finding size may be overkill...
// is this just matter of preference?  
Here's the whole program:
CPP / C++ / C Code:
#include <iostream> //  fstream-1.0.0.cpp  (experiments)
#include <fstream>
using namespace std;

int mygetfstat1(fstream * fp);
streamsize mygetsize(fstream * fp);

int main()
{
  char c, i;
  streamsize size;

  // declare and open file for reading and writing (and binary mode for blocks)
  fstream fh("infile", ios_base::in | ios_base::out | ios_base::binary);
  if ( (fh.rdstate() & ios::failbit ) != 0 )
    cerr << "Error opening 'test.txt'\n";

  cout << "--- test fstatus: --- #1: ";
  mygetfstat1( &fh ); //////////  is good

  fh << "Now is the time \nfor all good men \nto come to the aid of their party." << endl;

  cout << "--- test fstatus: --- #2: ";
  mygetfstat1( &fh ); //////////  is good!

  fh.seekp( 0, ios::beg );

  cout << "--- test fstatus: --- #3: ";
  mygetfstat1( &fh ); //////////  is good

  size = mygetsize( &fh );
  //while(( c = fh.get()) != EOF)  //this sets eof and fail errors, must clear()
  for(i = 0; i < size; i++)       // this works ok 
  {
    c = fh.get();
    cout.put(c);
  }

  cout << "--- test fstatus: --- #4: ";
  mygetfstat1( &fh ); //////////  is 'eof fail' if I use get() till EOF method

  //fh.clear();              // This clears any stream error-bits set

  // then I move and overwrite at a desired location...
  // this will not write anything if fh is in error state  
  fh.seekg( 0, ios::beg );
  fh.seekp( 35, ios::beg );
  fh << " ...dude, this gets complicated... "  ;                                 
  cout << "--- test fstatus: --- #5: ";
  mygetfstat1( &fh );  //////////  

  size = mygetsize( &fh );
  for(i = 0; i < size; i++)
  {
    c = fh.get();
    cout.put(c);
  }

  cout << "--- test fstatus: --- #6: ";
  mygetfstat1( &fh );

 fh.close();
  return 0;
}

/*          */
int mygetfstat1(fstream * fp)
{
  bool iostate;

  iostate = fp->rdstate();
  cout << " fp.rdstate() is: " << iostate << " which means: ";
  if( ( iostate ) == 0 )
    cout << "GOOD! ";
  if( ( iostate & ios::badbit  ) == 1 )
    cout << " BAD! ";
  if( ( iostate & ios::eofbit  ) == 2 )
    cout << " EOF! ";
  if( ( iostate & ios::failbit ) == 4 )
    cout << " FAIL! ";
  cout.put(0x0a);
  return iostate;
}

streamsize mygetsize( fstream * fp)
{
  streamsize curr, size;
  curr = fp->tellg();
  cout << "curr= " << curr << endl;
  fp->seekg( 0, ios::end);
  size = fp->tellg();
  cout << "size= " << size << endl;
  fp->seekg( 0, ios::beg);
  return size;
}

/*
[url]http://www.cplusplus.com/reference/iostream/ios/rdstate.html[/url]
Return Value
flag value      indicates
eofbit   End-Of-File reached while performing an extracting operation on an 
          input stream.
failbit  The last input operation failed because of an error related to the 
          internal logic of the operation itself.
badbit   Error due to the failure of an input/output operation on the stream
          buffer.
goodbit  No error. Represents the absence of all the above (the value zero).
*/
Last edited by Howard_L : 27-Aug-2008 at 11:07.
  #2  
Old 27-Aug-2008, 17:33
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,821
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: using fstream::fstream , question about error status


Quote:
Originally Posted by Howard_L
...
...which is the better...
Well, that's an easy one to answer. The answer is, "it depends."

But, seriously, if you have been cruising around in the file, reading and/or writing, and you just want to get to the end of the file to write some new stuff without reading everything (and throwing it away), why not just do something like the following?

CPP / C++ / C Code:
    fh.seekp(0, ios::end);


Read up on seekg() and seekp()


Regards,

Dave
  #3  
Old 27-Aug-2008, 21:32
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 582
Howard_L has a spectacular aura aboutHoward_L has a spectacular aura about

Re: using fstream::fstream , question about error status


I guess that's an 'it doesn't matter'?
I understand seekg() and seekp() usage and use it some in the program above.
I suppose I was just wondering if it was undesireable to read to EOF and get that error bit.
Thanks
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Please, This is Difficult question try for me! bcompt143 C++ Forum 4 24-Aug-2008 10:14
Question about locking surfaces to directly access pixels, SDL. george89 C++ Forum 0 18-Jun-2006 22:16
question on sorting items is not clear...plz help me gvsivannarayana C Programming Language 0 28-Mar-2006 01:26
Question about sprintf() shvalb C Programming Language 1 23-Feb-2005 05:28
question of practice magiccreative C++ Forum 1 06-Feb-2004 08:17

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:16.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.