File Operations | get and place the pointer

I have some questions about file manipulation;

a.) I'm a little confused about getting and delivering a pointer in C ++. I am showing the correct position of the get pointer and place the 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, which get a pointer to a pointer, always show the correct position?
ii.) If you know more about this topic, can you show / give me some points, I must be careful when I use them (if any)

b.) is

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

equal

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

Platform: linux File Format: Binary

+4
source share
1 answer

a) This is wrong. File streams support a single file pointer, both for input and output. Both seekg and seekp do the same. The reason is that there are two different functions: the iostreams interface is general, it can be used for devices that have separate put and get buttons.

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 recording, the output sequence cannot be recorded.

- The joint file position is supported 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 
+4
source

Source: https://habr.com/ru/post/1380156/


All Articles