How to insert char in middle of text file?

ぃ、小莉子 提交于 2019-12-11 14:18:36

问题


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



来源:https://stackoverflow.com/questions/51663960/how-to-insert-char-in-middle-of-text-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!