我以前一直用c编程,用的输入输出主要都是scanf,printf,fscanf,fprintf之类。跳文件指针,自然使用fseek。
最近尝试c++,遇到了各种各样的问题,其中一个是,使用fstream的getline函数后,如果没能读完一行,无论哪个重载,都会把该fstream的文件指针跳到-1。
显然我一开始不是这么理解这个函数的。我觉得这个函数应该是读到哪里就把指针跳到哪里——不管这一行读没读完。
#include<iostream>
#include<fstream>
#include<stdlib.h>
using namespace std;
int main()
{
fstream fs;
fs.open("input.txt",ios::in);
if(!fs.is_open())
return 0;
char buf[500];
cout<<"tellg:"<<fs.tellg()<<endl;
cout<<"tellp:"<<fs.tellp()<<endl;
fs.getline(buf,50);
//fs.getline(buf,',',50);
cout<<"tellg:"<<fs.tellg()<<endl;
cout<<"tellp:"<<fs.tellp()<<endl;
fs.close();
system("pause");
}
如上所示的代码。
我们给一个输入文件,开头足够长,远大于50个字符。
在有足够长的文件的时候,运行结果是:
如果把夸张的第一行删掉,运行结果就变成了。
我试着简单百度了一下,没查到什么好的说法。
教科书上推荐使用fstream的非成员函数getline,也就是string头文件里的getline。然而这个版本的getline有四个重载,全都会把这一行读完,只能规定终止字符,不能规定读多少就停。另外,这个是读到string里,不是读到char[]里。(或许正因如此才取消了字符数上限这个参数。我们可以大概领悟到,成员版函数文件指针置-1是报错的意思。)
因此,我采用了一个非常笨的办法:再封装fstream的getline,手动改成我需要的形式。
这个事情没那么简单。如果直接用seekg去移动的话,会发现根本移动不了,因为此时failbit已经被置1了。要想没这个问题,必须clear()。
不clear:
clear之后:
最后的测试代码。
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<string>
using namespace std;
void Status(fstream& fs)
{
cout<<"failbit:"<<fs.fail()<<endl;
cout<<"tellg:"<<fs.tellg()<<endl;
cout<<"tellp:"<<fs.tellp()<<endl<<endl;
}
void Getline(fstream& fs, char* buf, int count = 50, char delim = '\n')
{
ios::pos_type pos = fs.tellg();
Status(fs);
fs.getline(buf,count,delim);
if (-1 == fs.tellg())
{
fs.clear();
cout<<"fseek:"<<pos + (ios::pos_type)count<<endl;
fs.seekg(pos + (ios::pos_type)count,ios::beg);
fs.seekp(pos + (ios::pos_type)count,ios::beg);
Status(fs);
return;
}
return;
}
int main()
{
fstream fs;
fs.open("input.txt",ios::in);
if(!fs.is_open())
return 0;
char buf[500] = {0};
Status(fs);
Getline(fs,buf,50);
//fs.getline(buf,50);
//fs.getline(buf,',',50);
Status(fs);
fs.close();
system("pause");
}
需要可以把Status有关内容删去,就能直接用了。如果我这是不小心绕了弯路,忽略了更好用的函数,还请各位大神赐教。