Slight dilemma...

This is a discussion on Slight dilemma... within the C++ Programming forums, part of the General Programming Boards category; Ok, im making a program (duh) and i need it to store a list of members, i was going to ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434

    Slight dilemma...

    Ok, im making a program (duh) and i need it to store a list of members, i was going to do this in a .txt file an dhave the program read and write to the file. Well the dilemma i came up with is suppose i need to delete a member from the file. How do i do that? By that i mean pick out a specific line from the file and delete it. Thanks!

  2. #2
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    I had to do this with this certain program I made to hold quotes or whatever. First you should use vectors. Store all the text files contents into a vector. Then when you want to remove a line from the "database" do the "vector.erase(linenumber)" function. Then your vector will hold all the data you want EXCLUDING the just erased spot in the vector. Now just clear the text file and rewrite the contents.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  3. #3
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Reference to a good thread that deals with what I was talking about:
    Better way to delete one line from file
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  4. #4
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Ok thanks, ill take a look, does it say how to implement vectors though (its taking a long time to load)? I have never worked with them before, ill look, thanks again!

  5. #5
    Meganan
    Join Date
    Oct 2005
    Posts
    13
    Hey seriously on this post is there any way to find and erase/replace a string from a text file?? I can't figure out how to do it without taking the whole string out of the text file clear the file doing a strstr() and then placing it all back into the file.

  6. #6
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Function I wrote for a program I made that erases a user-selected line:
    Code:
    string vtext;
    vector<string> dbase;
    vector<string>::iterator forward;
    BOOL RemoveFromDbase(int index)
    {
    ifstream vin("qlist.dat");
    if(!vin.is_open())
    {
    return FALSE;
    }
    if(vin.is_open())
    {
    while(getline(vin,vtext))
    {
    dbase.push_back(vtext);
    }
    }
    vin.close();
    dbase.erase(dbase.begin()+index);
    ofstream vout("qlist.dat");
    for(forward=dbase.begin();forward!=dbase.end();forward++)
    {
    vout<<*(forward);
    if(forward+1!=dbase.end())
    {
    vout<<endl;
    }
    }
    vout.close();
    dbase.clear();
    return TRUE;
    }
    I just dug that up from one of my past projects. This was used where all the contents of "qlist.dat" were displayed in a listbox and the user selected which selection to remove. Then it retrieves which one the user selected and calls this function, passing the number of the selection to this function. Hope that helps.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  7. #7
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Code:
    //program to delete a line from a text file
    #include <iostream>
    #include <fstream>
    
    
    
    using namespace std;
    
    int main()
    {
        cout<<"Enter the name of the file you wish to alter"<<endl;
        cout<<"E.g  myfile.txt"<<endl;
        cout<<">>";
        
        char name[81];
        cin>>name;
       
        char ch;
        cout<<"\n\n\n";
        ifstream file_pointer(name);
        int count_lines=1;
        cout<<"1:";
        
       
        while (file_pointer.get(ch)) 
        {
            cout<<ch;
            if(ch=='\n') //newline
            {
                count_lines++;
                cout<<count_lines<<":";
                
            } 
           
        }
        file_pointer.close();
      
        cout<<"\n\nThere were "<<count_lines<<" lines in your file.";
        cout<<"\nWhich line would you like to delete?"<<endl;
        cout<<">>";
        
        int line;
        cin>>line;
        
        cout<<"Deleting line "<<line<<endl;
    
        char chr;
        ofstream f_out("amend.txt");
        ifstream f_in(name);
        int lin=1;
        while (f_in.get(chr)) 
        {
            
            
            if(chr=='\n') //newline
            {
                lin++;
        
            } 
            if (lin!=line)
            {
                f_out<<chr;//write to file
            }
            
            else if(lin==line)
            {
                
                cout<<chr; //identify the line to 
                           //be deleted
            }    
        }
        f_out.close();   
        f_in.close();     
            
            
        cout<<"\n\nYour new file 'amend.txt' has been created";
        cout<<"\nCheck in your current directory for this file";
        
        cin.get();
        cin.get();
       
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. High Scores - A Dilemma
    By CrazyNorman in forum Game Programming
    Replies: 5
    Last Post: 12-28-2006, 08:01 PM
  2. Help removing slight switch flicker
    By Magos in forum Windows Programming
    Replies: 1
    Last Post: 05-12-2006, 04:20 AM
  3. slight problem
    By duvernais28 in forum C Programming
    Replies: 4
    Last Post: 02-03-2005, 10:03 AM
  4. dilemma
    By axon in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 12-03-2003, 12:28 PM
  5. slight problem just can't see the answer
    By anthonye in forum C++ Programming
    Replies: 1
    Last Post: 07-05-2002, 08:45 AM

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