How to search a data in a txt file using fstream?

HOw to search a specific data in a txt file? im using fstream for a permanent storage of the data , i want a option to remove a data if its not needed anymore , is there any chance i can do that?
Not specific to C++ I would
a) read the file record by record, b) output to a temp file only what passes your filter "still keep it" removing the not needed data, c) iterate a) and b) until done then close both files, d) rename original to something unique (XYZ), rename temp file to original name, and when successful discard original file (that was just renamed to XYZ).
Howww can you show it on code?

HEres the code i made on line 100


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <iomanip>
#include <string>
using namespace std;

struct reserve
{
    string name;
    string date;
    string time;
    string operation;

};
int main()
{
    reserve res;
  fstream myfile;

  char choice;

 myfile.open("Reservation.txt",ios::in|ios::out|ios::app);
  choices:
  cout<<"\t\t\tReservation"<<endl;
  cout<<"Choice the letter of your choice:"<<endl;
  cout<<"[A]. View Reservation\n[B]. Add Reservation\n[C]. Remove Reservation\n[D]. Exit Program"<<endl;
  cout<<"\nChoice:";

  cin>>choice;
cin.ignore(10000, '\n');


  if  (choice=='A'||choice=='a')
  {
      system("cls");
cout<<"\n\n\t\t\t\tLIST OF RESERVATION";
     string output;
     myfile.seekg(0,ios::beg);
 cout<<endl<<endl;
     cout<<left<<setw(25)<<"NAME";
     cout<<left<<setw(25)<<"DATE";
    cout<<left<<setw(20)<<"TIME";
     cout<<left<<setw(20)<<"OPERATION";
     cout<<"\n";
     while (getline(myfile, output))
  {
      cout<<endl;
	cout<<left<<setw(25)<<output;
	std::getline(myfile, output);
	cout<<left<<setw(25)<<output;
	std::getline(myfile, output);
	cout<<left<<setw(20)<< output;
	std::getline(myfile, output);
	cout<<left<<setw(20) << output;
	cout<<endl;



  }

  cout<<endl<<endl;
  cout<<"\t\t\t\t******************"<<endl;
  system("pause");
   system("cls");
  cout<<"\n\n\n \t\t\t\tT H A N K   Y O U! \n\n\n"<<endl;
  system("pause");

  }
  else if(choice=='B'||choice=='b')
  {

  system("cls");

  cout<<"\n\t\t\t\tADD A RESERVATION\n"<<endl;


        cout<<"\n\tEnter the name:";
        getline(cin,res.name);


        cout<<"\n\tEnter the date:";
        getline(cin,res.date);
        cout<<"\n\tEnter the time:";
        getline(cin,res.time);
        cout<<"\n\tEnter the operation:";
        getline(cin,res.operation);

        myfile<<res.name<<"\n"<<res.date<<"\n"<<res.time<<"\n"<<res.operation<<"\n";

        system("cls");
        cout<<"\n\n\n\t\t\t\tS U C C E S S F U L L Y  A D D E D\n\n\n"<<endl;
        system("pause");
 system("cls");
        goto choices;



  }
  else if(choice=='C'||choice=='c')
  {
  string x,del;

  cout<<"Remove:";
  cin>>x;

  myfile.seekg(0,ios::beg);

  int ctr =1;
  while(getline(myfile,del))
  {
    if (ctr==1)
    {
        if(del==x)
        {
            cout<<del;
          getline(myfile,del);
          cout<<del;
          getline(myfile,del);
          cout<<del;
          getline(myfile,del);

          ctr=0;

        }else
        {
        ctr++;
        }
        ctr++;
    }
  }

  }
  else if(choice=='D'||choice=='d')
  {
    return 0;
  }
  else
  {
      system("cls");
      goto choices;
  }





}
@CoolAvocado
Your code is a nice interactive tool, showing the user what he may do. Alas, for Remove: I have no idea what to enter, there is no hint neither which entries are available for removal, nor if it is the name, date, time, operation, or anyone of it to select the entry to discard.
That is one thing, the other is, how may entries in struct reserve be identified? I assume something like if (res.name() = x) ... -- guesswork, best you look here: http://www.cplusplus.com/doc/tutorial/structures/
THankkkkk you @MikeStgt


i think i already try the if statement but different condition Thankkk youuu for help and responseeeeeee I really appreciate ittt

THANK YOU SO MUCH SIR
CoolAvocado, however I like your nickname, I’m bound to say if you get on spreading the same code in several posts every day, you’ll soon become annoying.

You are not guarantee you’ll get the entire solution to your homework, no matter how many time you ask for it, so I think the most of your effort should be better put into finding different solutions.

What I’d do (and this is a personal preference) is:
- I’d read the entire file into a std::vector<reserve>;
- I'd close that file;
- I’d enquire the user of what s/he want to do and perform that operations in memory
(I mean, in case of deletion, I’d remove the data from the std::vector);
- I’d open the same file again for writing and I’d save the entire std::vector (overwriting the previous information).

Maybe someone else would propose a different solution, but the best one is the one you find by your own.
- I’d open the same file again for writing
That way you avoid the renaming old/write new/if no error erase old, leave this drudgery for the OS.
Topic archived. No new replies allowed.