关于ifstream的简单问题

dreadknight3 2007-05-06 05:42:18
怎样才能实现让ifstream流的文件指针往回移动一个字符?
...全文
286 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuyunliuhen 2007-05-22
  • 打赏
  • 举报
回复
// basic_istream_seekg.cpp
// compile with: /EHsc
#include <iostream>
#include <fstream>

int main ( )
{
using namespace std;
ifstream file;
char c, c1;

file.open( "basic_istream_seekg.txt" );
file.seekg(2); // chars to skip
file >> c;
cout << c << endl;

file.seekg( 0, ios_base::beg );
file >> c;
cout << c << endl;

file.seekg( -1, ios_base::end );
file >> c1;
cout << c1 << endl;
}
jixingzhong 2007-05-22
  • 打赏
  • 举报
回复
istream.seekg (1, ios::cur); //当前位置进行相对移动
jixingzhong 2007-05-22
  • 打赏
  • 举报
回复
Enumeration seekdir
Enumeration Position
beg Begging of stream
cur Current position of stream
end End of stream

第二个参数控制即可
taodm 2007-05-22
  • 打赏
  • 举报
回复
用seekg/seekp的双参版本呀。
dreadknight3 2007-05-22
  • 打赏
  • 举报
回复
试过 istream::seekg(1)
是把文件指针移动到1这个绝对位置
如果我要把文件指针移动到当前指针下一个位置该怎样做?
jixingzhong 2007-05-06
  • 打赏
  • 举报
回复
如果是 ofstream,
则使用这个:

seekp
Syntax:

#include <fstream>
ostream& seekp( off_type offset, ios::seekdir origin );
ostream& seekp( pos_type position );

The seekp() function is used with output streams, but is otherwise very similar to seekg().
jixingzhong 2007-05-06
  • 打赏
  • 举报
回复
seekg
Syntax:

#include <fstream>
istream& seekg( off_type offset, ios::seekdir origin );
istream& seekg( pos_type position );

The function seekg() is used with input streams, and it repositions the "get" pointer for the current stream to offset bytes away from origin, or places the "get" pointer at position.
P6689 2007-05-06
  • 打赏
  • 举报
回复
istream::seekg(-1)

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧