getline use

Function definition:

  getline(istream &in, string &s)

effect:

  •  When the terminal input string by string type in C ++, with the input character string can not solve the problem of space.

Features:

  Reads from the input stream of characters, the string variable stored until the occurrence of the following conditions:

  • Reads the file marks the end of '\ 0'
  • Reading a new line '\ n'
  • Achieve maximum string length

  If getline not read the characters will return false, it can be used to determine whether the end of the text file

Use:

ordinary:

1 string str;
2 getline(cin,str);
3 cout<<str;

Input text file:

 1 #include<iostream>
 2 #include<fstream>
 3 #include<string>
 4 
 5 using namespace std;
 6 
 7 int main()
 8 
 9 {
10     string buff;
11     ifstream infile;
12     ofstream outfile;
13     cout<<"Input file name: "<<endl;
14     cin>>buff;
15     infile.open(buff.c_str());
16 
17      if(!infile)
18         cout<<"error"<<buff<<endl;
19 
20     cout<<"Input outfile name: "<<endl;
21     cin>>buff;
22     outfile.open(buff.c_str());
23     
24     while(getline(infile, buff))
25         outfile<<buff<<endl;
26 
27     infile.close();
28     outfile.close();
29     return 0;
30 }

 reference:

https://blog.csdn.net/slience_perseverance/article/details/19819601

https://blog.csdn.net/l211390760/article/details/81031030

Guess you like

Origin www.cnblogs.com/Anber82/p/11351786.html