istream::getline
istream& getline(char* s ,streamsize n);
istream& getline(char* s,streamsize n,char delim);
1 // istream getline 2 #include <iostream> 3 using namespace std; 4 5 int main () { 6 char name[256], title[256]; 7 8 cout << "Enter your name: "; 9 cin.getline (name,256); 10 11 cout << "Enter your favourite movie: "; 12 cin.getline (title,256); 13 14 cout << name << "'s favourite movie is " << title; 15 16 return 0; 17 }
Getline
istream& getline(isstream is,string& str,char delim);
istream& getline(isstream is,string& str);
View Code
1 // getline with strings 2 #include <iostream> 3 #include <string> 4 using namespace std; 5 6 int main () { 7 string str; 8 cout << "Please enter full name: "; 9 getline (cin,str); 10 cout << "Thank you, " << str << ".\n"; 11 }
istream::get
int get();
istream& get ( char& c );
istream& get ( char* s, streamsize n );
istream& get ( char* s, streamsize n, char delim );
istream& get ( streambuf& sb);
istream& get ( streambuf& sb, char delim );
1 #include <iostream> 2 #include <fstream> 3 using namespace std; 4 5 int main () { 6 char c, str[256]; 7 ifstream is; 8 9 cout << "Enter the name of an existing text file: "; 10 cin.get (str,256); 11 12 is.open (str); // open file 13 14 while (is.good()) // loop while extraction from file is possible 15 { 16 c = is.get(); // get character from file 17 cout << c; 18 } 19 20 is.close(); // close file 21 22 return 0; 23 }

浙公网安备 33010602011771号