文件操作| get和put指针 [英] Operations on file | get and put pointer

查看:182
本文介绍了文件操作| get和put指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在操作文件时有一些问题;



a。)我对c ++中的get和put指针有点困惑。我显示get指针和put指针的正确位置。

  MyFile。 seekg(0,ios :: beg); 
MyFile。 seekp(-10,ios :: end);

索引:0 1 2 3 4 5 6 7 8 9 10 ... -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
__________________________________________________________________
^ ^
^ ^
^ ^
获取指针放置指针

Myfile。 get(character);
MyFile。 write(SomeString,4);
MyFile。 flush();

索引:0 1 2 3 4 5 6 7 8 9 10 ... -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
__________________________________________________________________
^ ^
^ ^
^ ^
获取指针放置指针

i。)Seekg和seekp总是保证,获得一个put指针总是显示正确的位置?

ii)如果你知道更多关于这个话题,你能显示/给我一些点(如果有的话)



b)

  FileIN。 seekg(1,ifstream :: cur); 

等于

  FileIN。 seekg(1,ios :: cur); 

平台:linux
文件格式:binary

解决方案

a)错了。文件流保持一个文件指针,用于输入和输出。 seekg seekp 做同样的事情。有两个不同的功能的原因是iostreams的接口是通用的,它可以用于有分开的put和get指针的设备。



[filebuf]:


特别是:



- 如果文件不是



- 如果文件未打开,则无法写入输出序列。



- 为输入序列和输出序列保持联合文件位置。


b)是,它们是相同的。



EDIT: p $ p> index:0 1 2 3 4 5 6 7 8 9 10 ... -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
_____________________________________________________________________
^文件指针

MyFile。 seekg(0,ios :: beg);

索引:0 1 2 3 4 5 6 7 8 9 10 ... -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
_____________________________________________________________________
^文件指针

MyFile。 seekp(-10,ios :: end);

索引:0 1 2 3 4 5 6 7 8 9 10 ... -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
_____________________________________________________________________
^文件指针

Myfile。 get(character);
//如果输入上一个操作并切换到输出,则必须同步/ flush;
//或上一个操作已输出,并切换到输入。
MyFile。同步 ( ) ;

索引:0 1 2 3 4 5 6 7 8 9 10 ... -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
_____________________________________________________________________
^文件指针

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
_____________________________________________________________________
^文件指针


I have some questions on manipulating on a file ;

a.) I am a bit confused about get and put pointer in c++. Do I show correct position of get pointer and put pointer.

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 pointer

i.) Are Seekg and seekp always guarentee that get an put pointer always shows correct position ?
ii.) If you know more about this topic, can you show/give me some point(s) I should be careful when I use them, ( if there is )

b.) Is

   FileIN . seekg ( 1, ifstream :: cur ) ;

equal to

   FileIN . seekg ( 1, ios :: cur ) ; 

Platform : linux File format : binary

解决方案

a) It's wrong. File streams maintain one file pointer, for both input and output. Both seekg and seekp do the same thing. The reason there are two different functions is that the interface of iostreams is generic, it can be used for devices which do have separate put and get pointers.

Quote from the standard [filebuf]:

In particular:

— If the file is not open for reading the input sequence cannot be read.

— If the file is not open for writing the output sequence cannot be written.

A joint file position is maintained for both the input sequence and the output sequence.

b) Yes, they are the same.

EDIT:

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-pointer

这篇关于文件操作| get和put指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆