A Program to demonstrate getline() function

From WikiKantila

Jump to: navigation, search
/** 1 getline ( intut_stream, str, delim ); 
  *   Extracts characters from intut_stream and stores them in str 
  *   until str.max_size() characters have been extracted, the end of 
  *   file occurs, or delim is encountered, in which case delim is 
  *   extracted from istr but is not stored in str 
  * 2 getline( Iter, str ) Inputs a string value for str as in the 
  *   preceding funcĀ­tion with delim = 
*/

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <iterator>

using namespace std;

int main () 
{
    string str;
    cout << "Enter string (EOL = $) : ";
    getline (cin, str, '$');
    cout << "Str is : " << str << endl;

    ifstream In("data.dat");
    vector <string> v;

    cout << endl << "Reading data from file" << endl;
    while ( ! In.eof() )
    {
    	getline (In, str);
    	v.push_back(str);
    }

    copy (v.begin(),v.end(),
    		ostream_iterator <string> (cout,"\n"));
    cout << endl;
    	

    return 0;
}

OUTPUT

Enter string (EOL = $) : This program demonstrate getline() and STL$
Str is : This program demonstrate getline() and STL

Reading data from file
Nitun Kumar
Amit Saurabh
Mohit Goel
Sudesh Kantila


Go back to C++ Programs

Personal tools