How to insert char in middle of text file?

2020-05-06 17:48发布

问题:

Firstly, I want to insert characters into a text file. I want to print the characters (DAD1) into the middle of the text file (HERE with DA D1) and the text file looks like this =>

@2100
AB CD EF 12 43 56
A3 B2 34 56 .....
...............78
@ffb4
FF FF FF FF(***HERE***)
@ffc0
FA 21 FA 21 ....

I tried to use fprintf but it prints at the end of the file. Here is my coding part =>

fp = fopen("testing.txt", "r+");

if (fp)
{
    while (c != EOF)
    {
        c = getc(fp);
        if (c == '@')
        {
            cout << c ;
            while (c != '\n')
            {

                c = getc(fp);
                cout << c ;

            }
            addr ++ ;
        }
        else
        {
            if (addr == 1)
              { ***DO SOMETHING***}

            else if (addr == 2)
             {
              char higha = hia.to_ullong() + ((hia.to_ullong() >= 10) ? 55 : 48);
              char highb = hib.to_ullong() + ((hia.to_ullong() >= 10) ? 55 : 48);
              char lowa  = loa.to_ullong() + ((loa.to_ullong() >= 10) ? 55 : 48);
             char lowb  = lob.to_ullong() + ((lob.to_ullong() >= 10) ? 55 : 48);
             fprintf(fp, "%c", higha);
             fprintf(fp, "%c", highb);
             fprintf(fp, " ");
             fprintf(fp, "%c", lowa);      
             fprintf(fp, "%c", lowb);
             }
         }
    }
}

I try to use SEEK_CUR but I don't understand how it works. Or have another way to do it???

回答1:

You cannot insert/remove data to/from the front or middle of an existing file, only to/from the end.

If your original file is small enough to fit entirely in memory, you can read the whole file into memory, manipulate it as needed, and then write it back out to a new file, overwriting the original.

Otherwise, you will need to read from the original file and write its data to a secondary file until you reach the position where you want to insert/remove data, then write/omit the desired data to/from the new file as needed, and then finish reading the original file and writing it to the new file. Then replace the original file with the new file.



回答2:

Unless you're working on a file larger than 1/2 of your RAM, you will need to load the text file into RAM, detect it's length there, modify it, then write it back to disk. If the file is large then you would need to make a file stream driver, but unless you're trying to prevent SSD trashing then your best bet is the Read-Modify-Write method.



回答3:

The other answers are suggesting loading the entire file into memory, so I thought I would leave a comment:

If memory is an issue, try the following:

  1. Iterate through file to determine its size (do not load into memory).
  2. Set file pointer to size/2 and read the latter half of the file into memory.
  3. Insert byte(s) at index of size/2.
  4. Append latter half of memory after inserted bytes.

Advantages: Can handle a file twice the size of your memory, less copying.

Disadvantage: More iteration