Reading and writing files at the same time?

Moved the question here. Suppose I want to store 1,000,000,000 integers and cannot use my memory. I would use a file (which can easily process so much data). How can I let him read and write at the same time. Using fstream file("file.txt', ios::out | ios::in ); does not create the file in the first place. But assuming the file exists, I cannot use read and write at the same time. What do I have in mind: Let the contents of the file 111111 Then, if I ran: -

 #include <fstream> #include <iostream> using namespace std; int main() { fstream file("file.txt",ios:in|ios::out); char x; while( file>>x) { file<<'0'; } return 0; } 

Should the contents of the file 101010 ? Read one character and then overwrite the next one with 0? Or was all the content read right away in some kind of buffer if there should not be a single 0 in the file? 1111110 ? But the content remains unchanged. Please explain. Thanks.

+4
source share
2 answers

Two pointers are saved in the filters: one of them and one for writing. If you are doing read / write operations, you need to explicitly set these pointers using the seeg and seekp member seekp . You will also find that formatted I / O can interfere with what you are trying to do, so you should use the get / put and read / write member functions instead.

See also Why can't I read and add using std :: fstream on Mac OS X? for more information on open modes with fstream.

+4
source

Two possible solutions that will be much more efficient: (i) use a 64-bit OS or (ii) use mmap .

% man mmap

+1
source

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


All Articles