我有一些关于操作文件的问题;
a.)我对c++中的get和put指针有点困惑。我是否显示了get指针和put指针的正确位置。
MyFile . seekg ( 0 , ios :: beg ) ;
MyFile . seekp ( -10 , ios :: end ) ;
index :0 1 2 3 4 5 6 7 8 9 10 ... -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
__________________________________________________________________
^ ^
^ ^
^ ^
get Pointer put pointer
Myfile . get ( character ) ;
MyFile . write ( SomeString, 4 ) ;
MyFile . flush ( ) ;
index :0 1 2 3 4 5 6 7 8 9 10 ... -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
__________________________________________________________________
^ ^
^ ^
^ ^
get Pointer put pointeri.)Seekg和seekp总是保证得到一个put指针总是显示正确的位置吗?
(二)如果你对这个主题有更多的了解,你能告诉我/给我一些要点吗?我在使用它们时应该小心,(如果有)
b.)是
FileIN . seekg ( 1, ifstream :: cur ) ;等于
FileIN . seekg ( 1, ios :: cur ) ; 平台: linux文件格式:二进制
发布于 2011-11-08 19:19:32
a)这是错误的。文件流为输入和输出维护一个文件指针。seekg和seekp都做同样的事情。之所以有两个不同的功能,是因为iostream的接口是通用的,它可以用于具有独立的put和get指针的设备。
引用自标准文件the
特别是
:
-如果文件未打开以供读取,则无法读取输入序列。
-如果文件未打开以进行写入,则无法写入输出序列。
-为输入序列和输出序列维护连接文件位置。
b)是的,它们是一样的。
编辑:
index :0 1 2 3 4 5 6 7 8 9 10 ... -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
_____________________________________________________________________
^ file-pointer
MyFile . seekg ( 0 , ios :: beg ) ;
index :0 1 2 3 4 5 6 7 8 9 10 ... -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
_____________________________________________________________________
^ file-pointer
MyFile . seekp ( -10 , ios :: end ) ;
index :0 1 2 3 4 5 6 7 8 9 10 ... -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
_____________________________________________________________________
^ file-pointer
Myfile . get ( character ) ;
// you must sync/flush if your last operation was input and you switch to output,
// or your last operation was output and you switch to input.
MyFile . sync ( ) ;
index :0 1 2 3 4 5 6 7 8 9 10 ... -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
_____________________________________________________________________
^ file-pointer
MyFile . write ( SomeString, 4 ) ;
MyFile . flush ( ) ;
index :0 1 2 3 4 5 6 7 8 9 10 ... -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
_____________________________________________________________________
^ file-pointerhttps://stackoverflow.com/questions/8049403
复制相似问题