![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Oct 2005
Posts: 48
Rep Power: 0
![]() |
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;
} |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Oct 2005
Posts: 48
Rep Power: 0
![]() |
oh yeah... is a good idea... thanks nnxion
|
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Sep 2005
Posts: 50
Rep Power: 3
![]() |
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.
|
|
|
|
|
|
#6 |
|
Professional Programmer
|
You mean like... grep -v ?
![]() |
|
|
|
|
|
#7 |
|
Programmer
Join Date: Sep 2005
Posts: 50
Rep Power: 3
![]() |
lol, i was sitting on windows so i didn't bother to man page it. Good to know
. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|