【c++】getline导致文件读入指针跳为-1

我以前一直用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个字符。

在有足够长的文件的时候,运行结果是:

如果把夸张的第一行删掉,运行结果就变成了。


我试着简单百度了一下,没查到什么好的说法。

(c++ 手册见, http://www.cplusplus.com/reference/istream/istream/getline/ ,也没什么好说法)

教科书上推荐使用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有关内容删去,就能直接用了。如果我这是不小心绕了弯路,忽略了更好用的函数,还请各位大神赐教。



  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值