Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 30th, 2006, 4:36 AM   #1
myName
Programmer
 
Join Date: Oct 2005
Posts: 48
Rep Power: 0 myName is on a distinguished road
Is there a way to delete a line from a file?

Is there a way to delete a line from a text file?

int	posEndIdx;
	int	ipos=0;
	string sLine;
	string sKeyWord;
	const string sDelim ( "]" );

	try
	{
		fstream myInputFile("D:\\Logs\\CMS\\UnSended\\upload.text");
			
		if( myInputFile.is_open() )
		{
			while( !myInputFile.eof() )
			{
				getline(myInputFile, sLine);
				
				if(sLine.empty())
					continue;

				posEndIdx = sLine.find_first_of(sDelim);
				sKeyWord = sLine.substr( posEndIdx+1, (sLine.length() - (posEndIdx+1)) );
				cout<<sKeyWord<<endl;

				//*******delete the line*******
			}

			myInputFile.close();
		}
	}
	catch(...)
	{
		cout<<"Exception"<<endl;
	}
myName is offline   Reply With Quote
Old May 30th, 2006, 6:16 AM   #2
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
You write back the whole file without the lines you don't want (i.e. you don't write those lines).
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old May 30th, 2006, 8:44 PM   #3
myName
Programmer
 
Join Date: Oct 2005
Posts: 48
Rep Power: 0 myName is on a distinguished road
oh yeah... is a good idea... thanks nnxion
myName is offline   Reply With Quote
Old May 30th, 2006, 9:33 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Actually, you can delete a line from a file and only have to write the (original) file from the point of deletion onward. Editing a file in place when you might put more back than you took out is where the problem arises. Easier then to just work with a temp, as nnxion suggests.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old May 30th, 2006, 11:19 PM   #5
Serinth
Programmer
 
Serinth's Avatar
 
Join Date: Sep 2005
Posts: 50
Rep Power: 3 Serinth is on a distinguished road
I wonder if you could do that with a couple of system commands in linux? ie. cat and grep, only use the reverse of a grep.
__________________
A girl talked to me once.

http://www.latestanime.com
Serinth is offline   Reply With Quote
Old May 30th, 2006, 11:23 PM   #6
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 290
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
You mean like... grep -v ?

andro is online now   Reply With Quote
Old May 30th, 2006, 11:38 PM   #7
Serinth
Programmer
 
Serinth's Avatar
 
Join Date: Sep 2005
Posts: 50
Rep Power: 3 Serinth is on a distinguished road
lol, i was sitting on windows so i didn't bother to man page it. Good to know .
__________________
A girl talked to me once.

http://www.latestanime.com
Serinth is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:44 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC