W
W
warchant2015-10-15 12:50:51
Database
warchant, 2015-10-15 12:50:51

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

5 answer(s)
A
AtomKrieg, 2015-10-15
@warchant

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);

You can't add or remove bytes without overwriting the file, consider that they are in a row, when you delete, all behind bytes are shifted to the left. If you really want to do it without rewriting at all, fix the size of the strings for the first and last name - this is how it is done in the database.

G
GavriKos, 2015-10-15
@GavriKos

Without rewriting - in any way. IO doesn't work like that.

O
Oleg, 2015-10-15
@0LLEGator

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.

M
MiiNiPaa, 2015-10-15
@MiiNiPaa

It is quite possible to replace n bytes in the middle of a file.
Add/remove without overwriting - no way.

A
Adamos, 2015-10-15
@Adamos

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 question

Ask a Question

731 491 924 answers to any question