[comp.lang.c++] help with "get"

vcr@ROUGE.EDRC.CMU.EDU (V C Ramesh) (10/13/90)

I am new to C++.  I am trying to make "get" provide the same
functionality that "fgets" provides in C...namely, to read in 
successive lines of input for processing.  However, I am 
having problems with the newline character, which, get(char *) 
skips on the first scan, and on the next scan sees as the first 
character, and reads in an empty line.  Thus only the first 
line is read in correctly, and all the other lines are skipped.
I have enclosed a test code and typical output.

Is "get" the right function for my purpose ?  If so, how can
I get over this '\n' problem ?  In C++, which function does
one normally use to read in a file, and process each line ?

Thanks for any help,
Ramesh
vcr@cs.cmu.edu
---------------------- TEST CODE ---------------------

 main(int argc, char* argv[])
 {
    filebuf fin;
    if (fin.open(argv[1],input) == 0)
            error("cannot open input file", argv[1]);
    istream from(&fin);

    char linebuf[80];

    while(from.get(linebuf,80,'\n') && !from.eof())    
      {
cout << "Read in this line: " << linebuf << "\n" ;  
getchar(); 
      }
}

------------ Input file ---------
thanks to everyone who responded.
I moved the function...
including string.h...
......
---------------- Output
Read in this line:   thanks to everyone who responded.
[CR]
Read in this line:
[CR]
Read in this line:
[CR]
^C  (I give up)
-------------------

Roy.Browning@p0.f506.n106.z1.fidonet.org (Roy Browning) (10/16/90)

In a message of <Oct 13 01:22>, V C Ramesh (1:106/88@fidonet) writes:
 >Organization: Carnegie-Mellon University, CS/RI
 >Message-ID: <10740@pt.cs.cmu.edu>
 >Newsgroups: comp.lang.c++

 >I am new to C++.  I am trying to make "get" provide the same
 >functionality that "fgets" provides in C...namely, to read in 
 >successive lines of input for processing.  However, I am 
 >having problems with the newline character, which, get(char *) 
 >skips on the first scan, and on the next scan sees as the first 
 >character, and reads in an empty line.  Thus only the first 
 >line is read in correctly, and all the other lines are skipped.
 >I have enclosed a test code and typical output.

Ramesh:

        Your problem is a "feature" of cfront (bug IMHO).  I spotted it months ago in the Zortech compiler and was surprised when it wasn't corrected.  But I can easily understand why, if cfront is the standard against which other comforming compilers are measured, then it is best to comform.

        I've been meaning to report this for months just never got around to it.  Mostly because I didn't know who to contact.  A simple fix is to modify "Stream.Hpp - sgetc()".  But if you do that then any code you produce is non-conforming.

                           A simple work around follows,

                                    Roy Browning

******************************************************************************

//////////////////////////////////////////////////////////
//       friend istream& operator>>(istream&,String&)   //
//                                                      //
//       Copies istream to the passed String.sstr.*s.   //
//                                                      //
//////////////////////////////////////////////////////////

istream& operator>>(istream& const is,
                    const String& const str)
{
     char cr;
     char buf[one_k];
     is.get(buf,one_k);                         // following this
     str.p->l = strlen(buf);
     str.p->s = new char[++str.p->l];
     memcpy(str.p->s, buf, str.p->l--);
     is.get(cr);                                // do this

     return is;
}