简体   繁体   中英

Insert in a file without overwriting without using a 2nd file using C/C++

The below are the contents in a text file.

name1: 1234
name2: 2000
name3: 3000

This is an existing text file and I want to replace one value(say 1234) with another value (say 12345) in the text file. so I placed the cursor at start of the value (here its the 7th position) . Then i used the following statement:

fprintf(filepointer,"12345\\n");

The resultant file is like

name1: 12345
ame2 : 2000
name3 : 3000

Its overwriting the 4 characters("1000") and a newline('\\n') and 'n' with 5 characters("12345") and a newline('\\n').

The solutions I know are:
1. Overwriting the entire file to add one extra character.
2. Copying each line in a linked list node and change the characters in the memory and write in the same file.
3. Create a temp file and copy the new characters to the desired place in the temp file and change the name of the temp file to source file name and delete the source file.

Also I tried adding carriage return '\\r' and windows format of EOF ('\\r\\n') , still the next line characters are overwritten. Also I expanded the file size using [SetEndOfFile][1] API and still I face the same problem. I searched many forums and found answers like "It is not possible to insert characters without overwriting".

Is there any solution just to insert characters without overwriting the characters in the middle of the file. or any logic to insert characters in a line and not affect the next line.
Thanks in advance.

Is it possible Using visual studio VC++ ??
Thanks:)

Is there any solution just to insert characters without overwriting the characters in the middle of the file. or any logic to insert characters in a line and not affect the next line.

No, what you ask for is not possible with any file system I've ever used.

Use method (3), a temp file. Use a temp file with a different, unique extension added/replaced, eg '.tmp', so that the temp files can be recognised on startup, then:

1) Get source file name, eg. 'source.txt'

2) Append, or replace a '.tmp' extension: 'source.txt.tmp'

3) Open the two files

4) Read the source, modify, write the temp

5) Close both

6) Delete source file

7) rename temp file

When you program starts:

1) search for files with '.tmp' extension.

2) if found, eg. 'source.txt.tmp', take the file name and remove the extension, eg 'source.txt'

3) Test if a file with that name exists

4) If so, delete the temp file

5) If not, rename the temp file

Result: if the process crashes, you end up with either an intact, unmodified source file, or an intact, modified source file.

A sequential file is ehh... just sequential . All the bytes follow each other with no notion of line or any other structure.

The only operations allowed by the underlying file system are (only speaking of writing):

  • replace bytes anywhere in the file
  • add bytes at the end of the file
  • truncate the file (or add random(*) bytes at the end by moving the end of file

There is no provision for insertion in the middle so no language will be able to do that (except by doing overwriting under the hood)

(*) if you move the end of file forward more than one logical block (file system sense) many file systems will create a hole that is a phantom block that does not use any space on disk and is read as null bytes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM