Home > Back-end >  C to TXT file to modify the characters, will affect the subsequent content?
C to TXT file to modify the characters, will affect the subsequent content?

Time:09-26

TXT content is as follows, for example, there are three lines (binary)
Test1 # # 100
Test2 # # 100
Test3 # # 100
I want to first line rewritten as test1 # # 99.5, three lines and keep the original format
The read/write pointer position to file after opening, use fstream and & lt; Test1 # # 99.5
Est2 # # 100
Test3 # # 100
Or three lines, but test2 first character be eaten... , have a few questions want to consult

Q1: can you see the original file contents as "test1 \ \ ntest3 ntest2 # # 100 # # 100 # # 100" such a string stored? (the feeling that I can understand why the bug appeared above)

Q2: solution 1: rewrite test1 before this line, the test2 to deposited in the temporary file and delete files at the end of all content, rewrite the test1 then copied back
(if the file number very much, I will rewrite the beginning lines 1, 2, efficiency is very low) the idea is feasible?

Q3: solution 2: new storage length distribution of test1 this line to stay to the length of the written string, ensure not affect the content after
This function (not operation, the c + +?) The idea is feasible?

The trouble ace to help me to answer, teach me

CodePudding user response:

A1: if you don't change the first line the number of characters, it is good to modify directly, if change the length, the entire file into memory, rewrite the back after changes, so database to set a good field size first, and then write the record,
Note type data? Index, after the changes if the length the same write back, if the length change, the original position invalid to write a piece, but it will have to pieces, like disk fragmentation, how to efficient processing is one of the core work of database of this kind of problem,
A2: stored in memory, and then write back faster than copy the file, the file can be mapped and can also be sector remaining space optimization of the clusters, which is more advanced, there are few applications of soft in so doing, you can feel the office software, you change a large file, then save, even change a characters are to be preserved for a long time,
A3: there is no such function you need to write their own, such as compressed to write back,

CodePudding user response:

A1: disk storage, the thinking of you sure
A2: can be used, the efficiency is not high
A3: feeling is the problem 2, the feeling of a temporary file into memory

CodePudding user response:

The whole file to write it again,
  • Related