C++ getline函数

C++ getline函数

  • cin

虽然可以使用 cin>> 运算符来输入字符串,但它可能导致一些需要注意的问题:

​ 当cin读取数据时,它会传递并忽略任何签到白色空格字符(空格、制表符、换行符)。一旦它接触到第一个非空格字符即开始阅读,当它读取到下一个空白字符时它将停止读取。

cin>>name;
//此时可以输入“Mark”或者“Twain”,但不能输入"Mark Twain"
//因为cin不能包含嵌入空格的字符串。
#include <iostream>
#include <cstring>

using namespace std;

int main(){
	string name,city;
	cout<<"Please input your name:";
	cin>>name;
	cout<<"Enter the city you live in:"; 
	cin>>city;
	cout<<"Hello,"<<name<<endl;
	cout<<"You live in "<<city<<endl;
} 

/*
Please input your name:Mark Twain
Enter the city you live in:Hello,Mark
You live in Twain

--------------------------------
Process exited after 8.963 seconds with return value 0
请按任意键继续. . .
*/

在这个示例中,用户根本没有机会输入city城市名。因为在第一个输入语句中当cin读取到Mark 和 Twain之间的空格时,它就会停止阅读,只存储Mark作为name的值。而Twine被存入键盘缓冲区,在第二个输入语句执行时,cin使用在键盘缓冲区中找到的剩余字符并存储Twine作为city的值,用户根本没有机会输入。

为解决这个问题,可使用getline 的C++函数,可读取整行,包括前导和嵌入的空格,并将其存储在字符串对象中。

  • getline 函数:
getline(cin,inputLine);
/*
cin 是正在读取的输入流
inputLine 是接受输入字符串的string变量的名称

使用getline的示例

#include <iostream>
#include <cstring>

using namespace std;

int main(){
	string name,city;
	cout<<"Please input your name:";
	getline(cin,name);
	cout<<"Enter the city you live in:"; 
	getline(cin,city);
	cout<<"Hello,"<<name<<endl;
	cout<<"You live in "<<city<<endl;
} 
/*
Please input your name:Mark Twine
Enter the city you live in:New York
Hello,Mark Twine
You live in New York

--------------------------------
Process exited after 21.05 seconds with return value 0
请按任意键继续. . .
*/
  • **istream中的 getline() 函数 **

除了 getline() 全局函数外,C++还提供一个同名的 getline() 成员函数,定义在istream类中,适用于读取指定文件中的一行数据。

getline() 方法定义在 istream 类中,而 fstream 和 ifstream 类继承自 istream 类,因此 fstream 和 ifstream 的类对象可以调用 getline() 成员方法。

当文件流对象调用 getline() 方法时,该方法的功能变为冲指定文件中读取一行字符串,有两种语法格式:

istream& getline(char* buf , int bufSize);
/*
第一种语法格式用于从文件输入流缓冲区中读取 bufeSize-1 个字符到 buf,或,遇到 \n 为止(哪个条件先满足就按哪个执行),该方法会自动在buf中读入数据的结尾添加'\0'
*/
istream& getline(char* buf , int bufSize , char delim);
/*
第二种语法格式和第一种的区别在于,第一个版本时读到\n为止,第二个版本是读到delim字符为止。\n或delim都不会被读入buf,但会被从文件输出缓冲流中取走。

以上两种格式中,getline() 方法都会返回一个当前所作用对象的引用,比如,obj.getline() 会返回obj的引用。
注意:若文件输入流中\n 或 delim 之前的字符个数达到或者超过bufSize 就会导致读取失败。
*/

示例:

#include <iostream>
#include <fstream>

using namespace std;

int main(){
	char c[40];
	ifstream inFile("读取对象文件路径",ios::in);
	if(!inFile){
		cout<<"error"<<endl;
		return 0;
	}
	inFile.getline(c,40);
	cout<<c;
	inFile.close();
	return 0;
}
/*
<?xml version="1.0"?>
--------------------------------
Process exited after 0.05902 seconds with return value 0
请按任意键继续. . .
*/
//若采用方法二,读到字母r就会停止读取
//inFile.getline(c,40,'r');
/*
<?xml ve
--------------------------------
Process exited after 0.05983 seconds with return value 0
请按任意键继续. . .
*/

读取文件中多行数据示例:

#include <iostream>
#include <fstream>

using namespace std;

int main(){
	char c[40];
	ifstream inFile("读取对象文件路径",ios::in); 
	if(!inFile){
		cout<<"Error!"<<endl;
		return 0 ;
	} 
	while(inFile.getline(c,40)){
		cout<<c<<endl;
	}
	inFile.close();
	return 0;
} 
posted @ 2023-03-19 21:00  ben犇  阅读(556)  评论(0)    收藏  举报