tags:

views:

109

answers:

3

Hello!

I have a problem that I want to insert and delete some chars in the middle of a file. fopen() and fdopen() just allow to append at the end.

Is there any simple method or existing library that allow these actions? Thanks in advance.

+5  A: 

There is no simple method. You have do it manually. For example:

  1. Read the chunk you want to insert before into memory
  2. Seek forward to new position
  3. Write the chunk you just read at new position
  4. Seek back to where you want to insert
  5. Write the new data.
richb
Thanks! So I have to use "fopen("1.txt","w");" and then fseek ? :) As I understand you, the answer is yes for my question.
Infinity
@Infinity: Use `fopen("1.txt", "r+")` which opens for reading *and* writing. Mode `"w"` overwrites any file that might have existed previously, leaving you with an empty file.
Greg Hewgill
ok, thank you :)
Infinity
+2  A: 

Use fseek function to move the file pointer to the appropriate location of the file and then you can perform a write there. But, for this you should know how many bytes from the end or begining of the file your preferred area for write is.

Jay
Thanks to you too.
Infinity
+1  A: 

As others have already told you, you have to do it manually, and use fseek in order to get to the place in which you have to insert or add characters. You can easily add new characters in the middle by doing the following:

  1. Go to the last byte of the file, and store the old file size of the file.
  2. Go to where you want to insert the new characters (say this is position): fread (old file size - position) bytes, and store them in a buffer.
  3. fseek to position again.
  4. fwrite your new characters.
  5. fwrite the buffer you previously read.

If you want to delete characters in the middle, then this is more tricky. Actually you cannot make a file shorter. You have two possibilities: in the first one, you just

  1. open the file and read the file skipping the characters you want to delete and store them in a buffer
  2. Close and re-open the file again with "b", so its contents is erased,
  3. Write the buffer and close the file.

In the second possibility, you:

  1. Read to a buffer the characters ahead of the ones you want to delete.
  2. fseek to the beginning of the characters you want to delete
  3. fwrite the buffer.
  4. Trim the rest of the file.

Point four is "tricky", because there is no standard (portable) way to do this. One possiblity is to use the operating system system calls in order to truncate the file. Another, simpler possibility is to just fwrite EOF in point 4. The file will be probably larger than it should be, but it will do the trick.

Baltasarq
Thanks a lot man, I think you know more things about this. Can we talk a bit about this somewhere else?
Infinity
I am interested in the topic of searching the position of a text in a file. Maybe there is a good way or do I have to test charachter by charachter?
Infinity
Searching in a text file is, yes, basically a matter of testing character by character. But that is another totally different topic: you can get the help of regular expressions, for example.
Baltasarq
Ok, thank you. :)
Infinity