本文介绍了fstream unix阅读问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在UNIX上尝试从二进制文件读取。
I am trying to read from binary file on UNIX. The file exists and has several data information in it.
该代码如下所示:
fstrean fstrHandler;
string strFileName;
char Buf[30000];
fstrHandler.open(strFileName.c_str(), ios::in | ios::binary);
fstrHandler.seekp(0, std::ios_base::beg);
std::cout<< "Posi before read= "<< fstrHandler.tellg()<<endl; //*** Show after running 0
fstrHandler.read (Buf, 400);
std::cout<< "Posi after read= "<< fstrHandler.tellg()<<endl; //*** Show after running 0
std::cout<< " gcount ()= "<< fstrHandler.gcount ()<< << endl; //*** Show after running 0
if (fstrHandler.eof ()) {
fstrHandler.clear();
}
读取后,我得到文件中的位置仍然为零,
After the read I get that the position in file is still zero zero, but the file is not empty.
推荐答案
尝试 seekg
,而不是 seekp
,文件中是否有400字节?如果你输入一个包含超过400字节的文件,这似乎工作正常。如果更少,那么 tellg
在读取报告-1后,但 gcount()
是正确的。
Try seekg
rather than seekp
, and is there 400 bytes in the file? this appears to work okay for me, if you input a file that contains more than 400 bytes. If less, then the tellg
after read reports -1, but gcount()
is correct.
此外,打开文件后,测试该文件是否确实打开,例如
Also, after opening the file - test to see if the file was indeed opened e.g.
if (fstrHandler)
{
// do stuff
}
else
std::cerr << "foo bar" << std::endl;
这篇关于fstream unix阅读问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!