replace line in a file C++

后端 未结 4 1696
被撕碎了的回忆
被撕碎了的回忆 2020-12-30 15:40

I\'m trying to find a way to replace a line, containing a string, in a file with a new line.

If the string is not present in the file, then append it to the file.

4条回答
  •  难免孤独
    2020-12-30 16:18

    To replace the last two lines in your file:

    • Use fopen() to open the file
    • Get the current file position with fgetpos()
    • Store always the last two file position and read line by line
    • When you've reached the end of the file: The lower position is where you have to position to with fseek()
    • Now you can fprintf() your new lines to the file and close it with fclose() afterwards.

提交回复
热议问题