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