LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-17-2005, 01:39 AM   #1
CrazyHoboMan
LQ Newbie
 
Registered: Aug 2004
Distribution: Gentoo
Posts: 7

Rep: Reputation: 0
How do I delete a line in a file with fstream (C++)?


I have been spending a considerable amount of time trying to figure this out. I have been trying to write a program that creates a config file with entries separated by new lines, and have been trying to figure out how to delete a line -- like this:

Figure this out
Write such-and-such
Pay such-and-such

To:

Figure this out
Pay such-and-such

I want the line deleted determined by user input. Here is my code so far. Right now I just made it so that it would delete and rebuild each line; I have not discovered a more efficient way to write it. The only problem is it produces a garbled output to the text file. Can anyone help me figure out what I am doing wrong?

Code:
int remove()
{
        //Open file -- getconfigfile() gets the path for the config file
        fstream stodo;
        stodo.open(getconfigfile(), fstream::in | fstream::out );

        //char array line is stored in
        char line[255];
        short int choice;
        //function that lists items
        list(2);
        cout << "Choose an item to remove\n";
        cin >> choice;

        //Delete and rebuild lines before choice
        for(short int num = 1; num != choice; num++)
        {
                stodo.getline(line, 256);
                stodo.ignore(256, '\n');
                stodo << line;
        }
        //delete choice
        stodo.ignore(256, '\n');
        //delete and rebuild lines after choice
        while(!stodo.eof())
        {
                stodo.getline(line, 256);
                stodo.ignore(256, '\n');
                stodo << line;
        }
        //close file
        stodo << flush;
        stodo.close();
        return 0;
}
Any help would be appreciated. Apologies if I leave some detail out.

Last edited by CrazyHoboMan; 08-17-2005 at 12:41 PM.
 
Old 08-17-2005, 06:55 AM   #2
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
I know what you're doing wrong: you're not putting code tags around your code.

Code:
[ c o d e ]
my code
[ / c o d e ]
Delete the spaces.

I can't recall the exact C++ semantics of streams used both for input and output, so I can't say what you're doing wrong, but I can suggest an alternative solution:
1) slurp the file (read it into a vector).
2) delete an element from the vector.
3) write it back.

hth --Jonas
 
Old 08-17-2005, 12:47 PM   #3
CrazyHoboMan
LQ Newbie
 
Registered: Aug 2004
Distribution: Gentoo
Posts: 7

Original Poster
Rep: Reputation: 0
Don't really know much about vectors, but I'll look into it. Thanks for pointing me in some direction (I was getting a bit lost). Also i fixed the code block... didn't know it wouldn't format right.

Last edited by CrazyHoboMan; 08-17-2005 at 12:48 PM.
 
Old 08-18-2005, 02:06 AM   #4
enemorales
Member
 
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Rep: Reputation: 30
If the file is too big, using vectors wouldn't be a good idea, I think.

If the problem is reading/writing in the same file, you can always modify a little your program to create a copy of the file, delete the original and rename the copy...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
unix shell script:How to delete the first line in a file?? rche3252 Programming 6 03-03-2010 07:32 AM
[SOLVED] delete the end of each line in a file with sed angel115 Linux - Newbie 3 11-16-2005 04:41 PM
locate and delete a line from a file eilison Programming 3 05-17-2005 06:07 PM
Delete line from flat text file in C zaichik Programming 6 01-26-2005 06:16 PM
How to delete the first line of evrer .java file recursively ththomas Programming 2 03-08-2004 08:55 AM


All times are GMT -5. The time now is 02:56 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration