How to delete a specific line in a text file using C program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • animeprogrammer
    New Member
    • Jan 2014
    • 13

    How to delete a specific line in a text file using C program

    How's it going guys? Can you guys tell me if you know some codes to delete a text in a specific line in a text file.

    Example:
    This is line 1
    This is line 2
    this is line 3

    Removing line 2,

    This is line 1

    This is line 3
    //////////////////

    Every answer is much appreciated ;) Thank you..

    Regards,

    Anime Programmer
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    If the file is small

    Read the entire file into memory and close the file
    Reopen the file truncating it
    Write all the data in memory to the file excluding the line you want to delete.
    Close the file.

    If the file is large (or for added resilience)

    create a temporary file
    copy the file line by line to the temporary file skipping the line you want to delete.
    close the temporary file
    delete the original file
    rename/move the temporary file to the original file name

    Comment

    • animeprogrammer
      New Member
      • Jan 2014
      • 13

      #3
      thanks, thats what i did... it was hard at first, but i managed to do it...I copy the original and named it temp..just like you said..THANKS SO MUCH

      Comment

      Working...