Click here to Skip to main content
15,900,495 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
Please help me,I want to delete a particular line from a file by explicitly specifying the line number.
If I specify the line number, the corresponding line should be deleted from the file.
Can you please let me know how should I go about on this ?

Please help me.......
Posted
Updated 18-May-11 22:58pm
v2
Comments
ZeeroC00l 19-May-11 4:58am    
-- edited for grammar.

BR//
Harsha

Read the file line by line incrementing a counter variable and write that line to a temporary output file except for when the counter variable has the value of the line number you want to delete. After all lines have been read from the input file close input and output file. Then delete the input file and rename the temporary output file to the name of the original file.

Cheers and happy coding!

-MRB
 
Share this answer
 
v2
Comments
ZeeroC00l 19-May-11 5:13am    
:) Same answer.. Seems like you were faster than me :)
Manfred Rudolf Bihy 19-May-11 10:33am    
No vote?
ZeeroC00l 19-May-11 11:23am    
+5 from me :)
Jijesh Balakrishnan 19-May-11 5:32am    
is there any function to delete the line?any way thanks....
You can achieve this by following the steps below.

* Get the line number that needs to be deleted.

* Open the input file in Read Mode

* Open a temp file in write / append mode and set the counter variable to 0

* Read Contents from input file line by line Increment the counter by 1

* If the counter is matching with input line number then skip the line from writing into the output file.

* Continue untill end of file.

* Close both files.

* Remove the original file and move the temp file to original file.


This is the traditional way of doing what you want :)

BR//
Harsha
 
Share this answer
 
Comments
CPallini 19-May-11 5:26am    
I woulde say the traditional way is also 'the only viable' one.
:-)
ZeeroC00l 19-May-11 5:42am    
:) Couldn't agree more..
Jijesh Balakrishnan 19-May-11 5:32am    
is there any function to delete the line?any way thanks....
ZeeroC00l 19-May-11 5:42am    
No. There is no such functionality provided.
Manfred Rudolf Bihy 19-May-11 10:33am    
Take my 5!
You should read the file line by line (you may use getline[^] for the purpose), and save read lines to a memory buffer, for instance a vector of string (or to another, temporary, file), skipping the 'to be deleted' line. Finally overwrite the existing file with the memory buffer content (or with the temporary file content).
 
Share this answer
 
Comments
Ravi Sharma 2 19-May-11 5:20am    
this help u....
Jijesh Balakrishnan 19-May-11 5:32am    
is there any function to delete the line?any way thanks....
CPallini 19-May-11 5:41am    
Nope. There isn't.
You have to write your own (or steal someone else's code).
ZeeroC00l 19-May-11 5:43am    
-- (or steal someone else's code). --
:) Wouldn't that be something that we are not supposed to discuss in this forum :P
CPallini 19-May-11 5:48am    
Well, you already gave the code to him.
One could also consider doing an in-place copy of the contents after the deleted line. Requires a little more logic, but can be significantly faster.
 
Share this answer
 
Comments
CPallini 19-May-11 5:45am    
The faster way, I guess, is minimizing the number of file I/O calls, i.e. read the whole file, scan the buffer for the line to be deleted and write the two blocks of the read buffer (without the deleted line) back to the disk.
Niklas L 19-May-11 6:19am    
I was thinking memory mapping and block copying.
Manfred Rudolf Bihy 19-May-11 10:32am    
Sounds definitely better! 5+
Niklas L 20-May-11 2:52am    
Thank you!
Albert Holguin 19-May-11 10:46am    
I'm surprised everyone suggested the slowest possible method, my 5 for suggesting some optimized code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900