Delete a Line from a file in C Language

前端 未结 3 1809
故里飘歌
故里飘歌 2020-11-28 13:22

I want to delete certain lines in a file and insert certain lines in the same file based on whether certain parts of the line match a specified string. Is there a way of doi

3条回答
  •  独厮守ぢ
    2020-11-28 14:18

    If the file is not big enough for processing on the RAM, then you can use Linked list of strings, where each node represent a Line, that is node of the linked list is created based on the '\n' character and then you can do insert operations and delete operations on the linked list as necessary and then you can overwrite on the same file using the linked list.

    For Example, mytext.txt

    This is a test file

    A line has to be added above

    This line has to be deleted

    Now when you create a linked list of above file, it would be like

    [This is a test file] -> [A line has to be added above] -> [This line has to be deleted] -> [NULL]

    Insert Operation will change the linked list to

    [This is a test file] -> [This is a new line] -> [A line has to be added above] -> [This line has to be deleted] -> [NULL]

    Delete Operation will change the linked list to

    [This is a test file] -> [This is a new line] -> [A line has to be added above] -> [NULL]

    Now you can write the linked list to the mytext.txt file with '\n' Character in the end of every node.

    The Final File would be, mytext.txt

    This is a test file

    This is a new line

    A line has to be added above

提交回复
热议问题