Answer the question
In order to leave comments, you need to log in
How to change a line in a file?
Suppose there is a *.txt file that contains
FirstName|LastName in each line
. Let's assume that the size of the entire file is much larger than the amount of RAM (we cannot read the entire file into memory). Kind of like a database.
I need to change (!) one line from the middle of the file. Suppose I found this line and the cursor is at the beginning of it. The question is, with what tools in C++ can I change this line without overwriting the entire .
Is it possible to replace / remove n bytes from the middle of a file without overwriting the entire file in C++? How to do it?
UPD: That is, in order to change 1 byte in the middle of the file, I have to overwrite it all? :\
Answer the question
In order to leave comments, you need to log in
The replacement is done like this:
ofstream f("text.txt");
f.write("abcde",5);
f.close();
f.open("text.txt", ofstream::binary | ofstream::out | ofstream::in);
f.seekp(1);
f.write("zxc",3);
The question is how to remove tonsils per anus. You can BUT WHY? (So be perverted)
On a subject - direct low-level work with a disk.
It is quite possible to replace n bytes in the middle of a file.
Add/remove without overwriting - no way.
Let's say you've written a program that digs into clusters and replaces bytes with a direct write to disk.
Now imagine:
- that the user does not have admin rights, and the hell the system will allow him to directly access the disk;
- that the program is running under Wine, and it is impossible to call such functions
- that other file systems have been developed during the existence of your program, and the storage of files in them is different
- that the file is compressed on disk using Windows
- that the file is not located on a local disk, but on a connected network resource
- ...
Do you still want to go deeper?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question