Better way to delete one line from file

This is a discussion on Better way to delete one line from file within the C++ Programming forums, part of the General Programming Boards category; I have a little database I have created for my program's purposes and an option in my program is to ...

  1. #1
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532

    Better way to delete one line from file

    I have a little database I have created for my program's purposes and an option in my program is to remove a line from the database. The database is just a text file with a .dat extension so it is easily extracted with the getline() function. Anyways what I do is just re-read the whole file and exclude the line the user wants to remove. Then I erase the file's contents and rewrite all the lines that I stored into my buffer, the buffer should have all the lines except the excluded one. Then I just recreate my file and fill it with the buffer's contents. This method of doing this seems excessive and unnecessary to me. My question is, are their better more efficient ways of going about this? Thanks.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  2. #2
    Registered User
    Join Date
    May 2005
    Posts
    24
    the way I always do it is:

    - using tmpnam(), create a temp file
    - write new data to temp file
    - rename original file using tmpnam()
    - rename temp file to original name
    - delete the old file

    another reason I like this method is because it reduces the chances of losing data due to unexpected system or program termination.

  3. #3
    Banned
    Join Date
    Jun 2005
    Posts
    594
    load all your data into a vector<string>

    when they want to remove a line, delete the line from the vector,
    and when there done with changes overwrite the file with
    the vector. give me a few and ill write some example code.
    Last edited by ILoveVectors; 08-24-2005 at 10:37 AM.

  4. #4
    Banned
    Join Date
    Jun 2005
    Posts
    594
    here that code

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    int main()
    {
    	int deleterecord;
    	vector<string> myfile;
    	vector<string>::iterator forward;
    	string intext;
    	ifstream in("myfile.txt");
    	if( in.is_open() )
    	{
    		while( getline( in, intext, '\n' ) )
    		{
    			myfile.push_back( intext );
    		}
    	}
    	in.close();
    	
    	if( !myfile.empty() )
    	{
    		cout << "Which record to delete?" << endl;
    		int i = 1;
    		for(forward = myfile.begin(); forward != myfile.end(); forward++, i++)
    		{
    			cout << i << ":\t" << *(forward) << endl;
    		}
    		cout << endl << endl;
    		cout << "Enter a choice?  : ";
    		cin >> deleterecord;
    		myfile.erase((myfile.begin()+(deleterecord-1)));
    
    		ofstream out("myfile.txt");
    		for(forward = myfile.begin(); forward != myfile.end(); forward++)
    		{
    			out << *(forward) << endl;
    		}
    		out.close();
    	}
    	else
    	{
    		cout << "Sorry there are nothing in the Vector!" << endl;
    	}
    	cin.get();
    	return 0;
    }
    this is myfile.txt before
    Code:
    this is line1
    this is line2
    this is line3
    this is line4
    this is line5
    this is line6
    this is what it looks like after running to program
    and choosing option #1
    Code:
    this is line2
    this is line3
    this is line4
    this is line5
    this is line6
    Last edited by ILoveVectors; 08-24-2005 at 10:57 AM.

  5. #5
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Thanks, ILoveVectors, that way of doing it is way better than mine. I think I'm starting to love vectors... lol.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    have a little database I have created for my program's purposes and an option in my program is to remove a line from the database. The database is just a text file with a .dat extension so it is easily extracted with the getline() function. Anyways what I do is just re-read the whole file and exclude the line the user wants to remove. Then I erase the file's contents and rewrite all the lines that I stored into my buffer, the buffer should have all the lines except the excluded one. Then I just recreate my file and fill it with the buffer's contents. This method of doing this seems excessive and unnecessary to me. My question is, are their better more efficient ways of going about this? Thanks.
    Yes, there are faster ways. Text files are simpler and code using them is usually more portable, but you could go to using a binary file. Then, instead of physically deleting the tokens, you mark them as deleted. Eventually, and this is iff your file gets really large, you might then collapse the delete lines, but by doing so as a background task, you might avoid having thte user really noticing. Another way to try is using multiple files or using a single large file with some type of index. All these techniques, however, kind of replicate the work of a real database and you'll probably better off using sqlite or any one of the other databases out there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21