By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,563 Members | 1,058 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,563 IT Pros & Developers. It's quick & easy.

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

P: 13
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
Feb 6 '14 #1

✓ answered by Banfa

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

Share this Question
Share on Google+
2 Replies


Banfa
Expert Mod 5K+
P: 8,916
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
Feb 6 '14 #2

P: 13
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
Feb 15 '14 #3

Post your reply

Sign in to post your reply or Sign up for a free account.