-1

i have been trying to come up with a code that deletes data saved in a text file but to no avail. How should it be done?? in c++ this is my code how can i improve it so that it deletes saved data may be entry by entry?

  #include<iostream>
  #include<string>
  #include<fstream>
  #include<limits>
  #include<conio.h>
   using namespace std;

  int main()

  {
  ofstream wysla;
  wysla.open("wysla.txt", ios::app);
 int kaput;

 string s1,s2;
 cout<<"Please select from the List below"<<endl;
 cout<<"1.New entry"<<endl;
  cout<<"2.View Previous Entries"<<endl;
  cout<<"3.Delete an entry"<<endl;
  cin>>kaput;
  switch (kaput)
 {

 case 1:

    cout<<"Dear diary,"<<endl;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
    getline(cin,s1);
    wysla<<s1;
   wysla.close();

   break;
   }
  return 0;
   }
Wysla
  • 5
  • 2
  • 5
  • 1
    data? you mean arbitrary line in a text file? you cannot in place – sherpya Apr 26 '13 at 12:55
  • 3
    There only one way to delete from the middle of the file. Read the whole file into memory, delete the part you don't want in memory. Write out the whole file from memory. – john Apr 26 '13 at 12:58
  • @ brad no it is not a home wak its apersonal project – Wysla Apr 26 '13 at 13:02
  • 1
    @john No, it's not the only way to do it, but it's certainly the simplest one. – piokuc Apr 26 '13 at 13:06
  • @piokuc which other way can be used – Wysla Apr 26 '13 at 13:12
  • @Wysla Without going into details, just note that you don't really need to keep in the memory the part of file before the part you want to modify. But what john suggested is the simplest, so I suggest you go for it – piokuc Apr 26 '13 at 13:14
  • 1
    @john That's not the way I usually do it, and in fact, it's not a very good way. The usual way is to copy the file to a temporary file, making the changes on the fly, as you copy. Then close the temporary file, _and only if_ the output stream is good after the close, delete the original, and rename the temporary. – James Kanze Apr 26 '13 at 13:56

2 Answers2

0

I can give you the fastest way I have used for the same purpose. Use the function http://www.cplusplus.com/reference/cstdio/fseek to go to exact location. Suppose you are keeping name in a file. Then the name list will be

Alex
Timo
Vina

When you delete Alex, insert an extra character prefix so that you can mark it as deleted

-Alex
Timo
Vina

You will not show it when necessary.

If you do not want to do this, you have to copy without that particular line. See help from Replace a line in text file. In your case you replace with empty string.

Community
  • 1
  • 1
pcbabu
  • 2,219
  • 4
  • 22
  • 32
0

Do it with the help of a vector.

//Load file to a vector:
string line;
vector<string> mytext;
ifstream infile("wysla.txt");
if (infile.is_open())
{
    while ( infile.good() )
    {
        getline (infile,line);
        mytext.push_back(line);
    }
    infile.close();
}
else exit(-1);

//Manipulate the vector. E.g. erase the 6th element:
mytext.erase(mytext.begin()+5); 

//Save the vector to the file again:
ofstream myfile;
myfile.open ("wysla.txt");
for (int i=0;i<mytext.size();i++)
    myfile << mytext[i];
myfile.close();
LovaBill
  • 5,107
  • 1
  • 24
  • 32