-
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!
-
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.
-
Reference to a good thread that deals with what I was talking about:
http://cboard.cprogramming.com/showthread.php?t=68988
-
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!
-
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.
-
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.
-
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();
}