找回密码
 注册
Simdroid-非首页
查看: 151|回复: 2

[2. C/C++/C#] 【求助】C++文件流如何返回文件开始?

[复制链接]
发表于 2004-6-28 07:23:12 | 显示全部楼层 |阅读模式 来自 加拿大
请问,C++文件流如何返回文件开始?
  
我现在是一次次关闭又打开。
C语言有个rewind, 但是c++的流怎么办?
  
ofstream inputfile("c:\\input.txt");
inputfile>>variable,,,,;
  
如何将文件转到头部重头输入?
  
inputfile.close();
发表于 2004-6-28 10:55:54 | 显示全部楼层 来自 美国

回复: 【求助】C++文件流如何返回文件开始?

Simdroid开发平台
  1. ostream&  seekp ( streampos pos );
  2. ostream&  seekp ( streamoff off, ios_base::seekdir dir );
复制代码

Set position of the put pointer.
  Sets the position of the put pointer. The put pointer determines the next location where to write in the buffer associated to the output stream.   
  
Parameters.
  
pos
The new position in the stream buffer. This parameter is an object of type streampos  
off
Value of type streamoff indicating the offset in the stream's buffer. It is relative to dir parameter.  
dir
Seeking direction. An object of type seekdir that may take any of the following values:  
ios_base::beg (seek from the beginning of the stream's buffer).  
ios_base::cur (seek from the current position in the stream's buffer).  
ios_base::end (seek from the end of the stream's buffer).  
Return Value.
  The function returns *this  
  
Example.
  
  1. // position of put pointer
  2. #include <fstream>
  3. using namespace std;
  4.   
  5. int main () {
  6.   long pos;
  7.   
  8.   ofstream outfile;
  9.   outfile.open ("test.txt");
  10.   
  11.   outfile.write ("This is an apple",16);
  12.   pos=outfile.tellp();
  13.   outfile.seekp (pos-7);
  14.   outfile.write (" sam",4);
  15.   
  16.   outfile.close();
  17.   
  18.   return 0;
  19. }
复制代码

  In this example seekp is used to move the put pointer back to a position 7 characters before the end of the first output operation. The final content of the file shall be:
  1.   This is a sample
复制代码
 楼主| 发表于 2004-7-1 10:14:03 | 显示全部楼层 来自 加拿大

回复: 【求助】C++文件流如何返回文件开始?

感谢。
不过,对于输入流,我如何在已经读入一些后再转到文件开始呢?
  
tellp()不是输入流的函数啊。
error C2039: 'tellp' : is not a member of 'ifstream'
  
ifstream inp('filename');
  
inp>> variables;;;
  
最后如何使inp转回文件开始呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Simapps系列直播

Archiver|小黑屋|联系我们|仿真互动网 ( 京ICP备15048925号-7 )

GMT+8, 2024-11-2 05:26 , Processed in 0.030348 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表