how to delete data from a file in c++
Posted on 2007-10-10
using namespace std;
int ism_countstr(const char *buf, const char * str);
string line, final_line, point_line, del_str, del_str2,source,delete_string,delete_string1;
int del_len, del_pos,del_pos1,del_len1,str_len,count=0;
string *points = new string;
const char* cmpstrvector;
cout<< "Enter the points to be deleted from the file:";
cout << "Enter the file name: ";
const char* filename = source.c_str();
cout<<"Enter the dimension:";
ifstream main_file(filename, ios::in);
while (getline(main_file, line, '\n')) /// read the file from start to end line by line into a STL string object
final_line += line;
final_line += " \n";
main_file.close(); /// close the file after reading is over.
//ifstream point_file("point_file.txt", ios::in); //read point file into a STL string object
//getline(point_file, point_line, '\n'); ////assumed that points to be deleted will be in a single line
cout<<"point_line :"<<erasepoints<<endl; ///that's why while loop is omitted here
cout<<"the string length is:"<<str_len;
del_len = delete_string.length(); ////find the length of the string to be deleted.
del_pos = final_line.find(delete_string,0); ////search for the string to be deleted.
if(del_pos >= 0 )
final_line.erase(del_pos,del_len); ////if found, erase the string.
cout<<"\n"<<erasepoints<<" is not found in main file."<<endl;
cout<<"\n final_line after :"<<final_line<<endl;
ofstream outfile(filename, ios::out); ////now open the file to write the modified string into file and close it.
I want to delete the points from the file. the file will contain numbers from 0 to 99. the points that should be deleted from the file should be given in the console. i can able to delete the points from the file but i want to divide the points into small substrings like
the points to delete from the file are like: 1 2 3 4 5 6 7 8 then if we give the dimension as 4
the sequence 1 2 3 4 should be deleted first and then it should search for 5 6 7 8 and delete that sequence..