public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* question on ifstream::getline()
@ 2006-07-28  2:55 Jim the Standing Bear
  0 siblings, 0 replies; only message in thread
From: Jim the Standing Bear @ 2006-07-28  2:55 UTC (permalink / raw)
  To: gcc-help

I have a question regarding ifstream.getline().  According to
cplusplus reference, ifstream.getline(buffer, num) is supposed to get
(num - 1) of characters or '\n', which ever comes first.  However, in
my case, if the line length is more than (num), it will kill the
stream by setting the fail status.  Is this behavior normal? Or is
this an odd behavior of g++? Thanks.  I am using g++ v3.4.6


int countLines(char* filename, bool verbose) {
  ifstream ifs;
  int lineCount = 0;
  char randtext[TEXT_WIDTH + 1];

  ifs.open(filename);

  if (!ifs) {
    cerr << "Error: unable to open " << filename <<endl;
    exit(1);
  }

  ifs.getline(randtext, TEXT_WIDTH);

  while (!ifs.eof()) {
    lineCount++;
    if (verbose) {
      cout << "Reading line " << lineCount << ": " << randtext << endl;
    }

    ifs.getline(randtext, TEXT_WIDTH);
    cout << ifs.tellg() << endl;
    if (int(ifs.tellg()) == -1) {
      cout << ifs.bad() <<endl;
      cout << ifs.eof() <<endl;
      cout << ifs.fail() << endl;  //////////// oddly enough, when
there is a long line, the fail bit will be set and the file goes into
an infinite loop...
      exit(-1);
    }
  }


  ifs.close();

  return lineCount;

}


-- 
--------------------------------------
Standing Bear Has Spoken
--------------------------------------

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2006-07-28  2:55 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-07-28  2:55 question on ifstream::getline() Jim the Standing Bear

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).