Advertisement

istream's getline()

Started by August 16, 2001 07:25 PM
2 comments, last by ANTZelda56 23 years, 6 months ago
Would someone please explain (and give an example) of how to use istream''s getline(char *pch, int nCount,char delimeter=''\n'') function?
And Unless Microsoft has changed any oftheir happy little games, your stuck with it.Patrick Nortin ~TechTV''s "The ScreenSavers"~
istream::getline(char*s, streamsize n, char delim=''\n'') is used to get a line from a stream. It extracts characters from the stream and stores them into successive locations in the array pointed by s. Characters are extracted until either (n - 1) characters have been extracted, the delimiter (parameter delim or ''\n'' if not specified) is found, or if the end of file or any error occurs in the input sequence.

If the delimiter is found it is extracted but not stored (meaning it is read from the stream, put not stored in the array). An ending null character is automatically appended after the data stored in s.

For example:

#include <iostream>using namespace std;int main() {  char name[256], title[256];  cout << "Enter your name: ";  cin.getline(name, 256);  cout << "Enter your favourite movie: ";  cin.getline(title, 256);  cout << name << "''s favourite movie is " << title;} 


If you want to extract a line from a stream into a string (and not an array as above), you have to use the std::getline(istream& istr, string& str).

Please note that if you use Visual C++, the C++ library contains a serious bug that results in getline reading one character too many. This holds for both the istream::getline member function and the std::getline template function. See my sig for a link to fixes for this bug (and others).

HTH
Advertisement
You don''t have a sig any more
http://www.dinkumware.com/vc_fixes.html
quote:
Original post by Kylotan
You don''t have a sig any more
http://www.dinkumware.com/vc_fixes.html


Ah, that''s pretty stupid of me Looks like I forgot to check the correct box. Anyway, my sig should be listed now...

HTH





Some useful C++ links:
Free multiplatform ANSI C++ Standard Library implementation
Visual C++ STL fixes
Visual C++ 6.0 noncompliance issues
C++ FAQ Lite

This topic is closed to new replies.

Advertisement