Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Is there a way to delete a line from a file? (http://www.programmingforums.org/showthread.php?t=10074)

myName May 30th, 2006 5:36 AM

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;
        }


nnxion May 30th, 2006 7:16 AM

You write back the whole file without the lines you don't want (i.e. you don't write those lines).

myName May 30th, 2006 9:44 PM

oh yeah... is a good idea... thanks nnxion

DaWei May 30th, 2006 10:33 PM

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.

Serinth May 31st, 2006 12:19 AM

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.

andro May 31st, 2006 12:23 AM

You mean like... grep -v ?

;)

Serinth May 31st, 2006 12:38 AM

lol, i was sitting on windows so i didn't bother to man page it. Good to know :).


All times are GMT -5. The time now is 7:54 AM.

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