您的位置:首页 > 编程语言 > C语言/C++

C++ 全局函数std::getline

2016-08-22 14:53 204 查看
经常会用到getline来读取文件,但是发现对于c和c++的getline之前没有一个很深入的认识,这里借鉴网上的一些资料总结一下。

1、在标准C语言中,getline函数是不存在的。在gcc编译器中,对标准c库进行了扩展,加入了一个getline函数。该函数的定义如下:

      
#include <stdio.h>
ssize_t getline(char **lineptr, size_t *n, FILE *stream);

2、标准C语言中虽然没有getline函数,但是有一个同样功能的fgets函数。fgets函数是c风格的按行读取的函数,读取时是读入换行符的,使用起来也还方便。c代码中建议使用此函数读取行。

3、c++标准库中全局函数getline。该函数定义如下:

     
istream& getline ( istream& is, string& str, char delim );
istream& getline (istream& is, string& str);


函数说明详见:http://www.cplusplus.com/reference/string/string/getline/

c++标准库中有getline函数功能说明

从输入流读入一行到string s

•功能:
–从输入流中读入字符,存到string变量
–直到出现以下情况为止:
•读入了文件结束标志
•读到一个新行
–如果getline没有读入字符,将返回false,可用于判断文件是否结束

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main()
{
string buff;
ifstream infile;
ofstream outfile;
cout<<"Input file name: "<<endl;
cin>>buff;
infile.open(buff.c_str());

if(!infile)
cout<<"error"<<buff<<endl;

cout<<"Input outfile name: "<<endl;
cin>>buff;
outfile.open(buff.c_str());

while(getline(infile, buff))
outfile<<buff<<endl;

infile.close();
outfile.close();
return 0;

}


ios类实现了方法 operator void*() const;详细见:http://www.cplusplus.com/reference/ios/ios/operator_bool/ 


而所有的输入流类都是从ios类派生而来的,所以都有方法 operator void*() const。而getline返回的是istream对象,在while循环中需要将这个返回的istream对象转换为bool类型的值,所以这里编译器会调用operator void*() const来做一个隐式转换得到一个void*的指针。如果failebit或badbit是true的话,则返回NULL指针(即0值),转换为bool类型即为false;否则就转换为非NULL指针,对应bool类型即为true值。


getline在读到文件结束标志后就停止读了,如果再继续调用getline则会报错返回一个NULL指针,while循环就不会继续读了。即能达到读完文件后再读的话就报错的效果。


http://www.cplusplus.com/reference/ios/ios/operator_bool/      中附上的例子如下:


// evaluating a stream
#include <iostream>     // std::cerr
#include <fstream>      // std::ifstream

int main () {
std::ifstream is;
is.open ("test.txt");
if (is) {
// read file
}
else {
std::cerr << "Error opening 'test.txt'\n";
}
return 0;
}


4、c++中不同的输入流类都定义了一个字函数getline,如std::istream::getline。但是这个子函数是c风格的,需要执行buffer的长度,当行的长度大于这个buffer长度时,状态位failbit设置为true了,getline读取就会出现问题。所以,不建议用输入流对象的子函数getline去读取行,而应该直接用c++的全局函数getline去读取行,简单好用,不用担心buffer长度的问题。

5、C++中读取一行的getline函数是不读入换行符的,而C语言中GCC编译器扩展的getline函数和fgets都是是读入换行符的。

总结:

按行读取文件就用c++全局函数std::getline吧,简单方便。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐