unix fstream 读取问题
我正在尝试读取 UNIX 上的二进制文件。该文件存在并且其中包含一些数据信息。
代码如下所示:
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();
}
读取后,我发现文件中的位置仍然为零零,但文件不为空。
I am trying to read from binary file on UNIX. The file exists and has several data information in it.
The code looks like this:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试
seekg
而不是seekp
,文件中有 400 字节吗?如果您输入包含超过 400 个字节的文件,这对我来说似乎没问题。如果小于,则读取后的tellg
报告 -1,但gcount()
是正确的。另外,打开文件后 - 测试文件是否确实被打开,例如
Try
seekg
rather thanseekp
, 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 thetellg
after read reports -1, butgcount()
is correct.Also, after opening the file - test to see if the file was indeed opened e.g.