A code to remove specific data from a file

HEre is my unfinished code hope you can help me with the removing system

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
#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");

     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;
  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')
  {



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


        cout<<"\n\tEnter the date:";
        getline(cin,res.date);
        cout<<"\nEnter 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')
  {

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





}
A few suggestions.

1. Start using functions to implement distinct functions. A single bloated main() is not the way to go.

2. NEVER use goto's for backwards jumps. Use a for / while / do loop to achieve that.

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
#include <iostream>
#include <fstream>
#include <cstdlib>              // was #include <stdlib.h>
#include <iomanip>
#include <string>
using namespace std;

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

void viewReservation(fstream &myfile) {
  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;
  }
}

void addReservation(fstream &myfile) {
  reserve res;
  cout << "\n\tEnter the name:";
  getline(cin, res.name);
  cout << "\n\tEnter the date:";
  getline(cin, res.date);
  cout << "\nEnter 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";
}

void removeReservation(fstream &myfile) {
}

#if 0
// You see, having created methods to act on a reservation, you're only half
// a step away from making it a proper C++ class.
class Reservation {
private:
  string name;
  string date;
  string time;
  string operation;
public:
  void view();
  void add();
  void remove();
};
#endif

int main()
{
  fstream myfile;
  myfile.open("Reservation.txt", ios::in | ios::out | ios::app);

  while ( true ) {
    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:";

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

    if (choice == 'A' || choice == 'a') {
        viewReservation(myfile);
        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') {
        addReservation(myfile);
        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");
    } else if (choice == 'C' || choice == 'c') {
        removeReservation(myfile);
    } else if (choice == 'D' || choice == 'd') {
        break;
    } else {
        system("cls");
    }
  }
}


> myfile.open("Reservation.txt", ios::in | ios::out | ios::app);
http://www.cplusplus.com/reference/ios/ios_base/openmode/
ios::app can be tricky stuff, especially if you're trying to remove things from the middle of the file.

If you're reading, then open for reading, then read and then close.
If you're writing, then open for writing, then write and then close.
If you're deleting records, then open the existing file for reading, open a new file for writing and then copy all the records you want to keep.

In other words, keep it simple (KISS).

If you try to do everything with a single open file, you're going to come unstuck.
can you show me the code for removing?



I dont know how to start

we didnt tackle about classes so i didnt want to use it .

i dont know using goto is bad ahahhaha sorryy i just saw it on some and it just work

is it ok if idont put an ios::app?

Thankyou Pls help me
PLS SHOW ME HOW pLSSSSSSSSSSSSSS T_T
I saw the answer in one of your other many threads on basically the same subject.
modify and write it back?


How ?

How to start it?
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
#include <iostream>
#include <fstream>
#include <cstdlib>              // was #include <stdlib.h>
#include <iomanip>
#include <string>
using namespace std;

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

void viewReservation(fstream &myfile) {
  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;
  }
}

void addReservation(fstream &myfile) {
  reserve res;
  cout << "\n\tEnter the name:";
  getline(cin, res.name);
  cout << "\n\tEnter the date:";
  getline(cin, res.date);
  cout << "\nEnter 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";
}

void removeReservation(fstream &myfile) {
}

#if 0
// You see, having created methods to act on a reservation, you're only half
// a step away from making it a proper C++ class.
class Reservation {
private:
  string name;
  string date;
  string time;
  string operation;
public:
  void view();
  void add();
  void remove();
};
#endif

int main()
{
  fstream myfile;
  myfile.open("Reservation.txt", ios::in | ios::out | ios::app);

  while ( true ) {
    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:";

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

    if (choice == 'A' || choice == 'a') {
        viewReservation(myfile);
        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') {
        addReservation(myfile);
        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");
    } else if (choice == 'C' || choice == 'c') {
        removeReservation(myfile);
    } else if (choice == 'D' || choice == 'd') {
        break;
    } else {
        system("cls");
    }
  }
}



the code run but didnt run the same way

In the part that i need to view all the reserved client broke, somehow it didnt show up the way It must
Last edited on
> myfile.open("Reservation.txt", ios::in | ios::out | ios::app);
Remove this line.

Then open the file in each function with a mode APPROPRIATE to the action being performed.
Im stuct with the problem
on to remove it

I just dont get it the thing about modify ,

pls show me using codes PLEASE PLEASE IM BEGGING
that is a load of code to do not much.

all you need is this, with the supporting code around it (variable creation etc). This is pseudocode!

while ( data = file.getline)
{
if (data != getridofme)
outfile << data; //implied else, do not write the bad line to the file.
}

if the bad data is not a full line you have to break it down more but that is the LOGIC you need.
The process behind the logic is to read the file into memory of the computer, change that, and write it back out into a new file with the updated contents.

Start there. Get this working. Once you understand it, then you can try something a little bigger like reading the original file into a container, updating it, and writing it back over the original file. And later we can talk about files that are too big to dump into memory all at once, but those are going to be many gigabytes in size on even a modest PC, and you can ignore this issue while learning the basics.

Modify the file is really modifying your data storage of the file, in other words. Say you put each 'word' into a list of strings, then you can remove some of the words from the list, then you can write it all back out again with the missing stuff gone. This is what you need to understand; you don't really delete data from a file 'in place' on the disk. Its a lot like how you delete an element from the middle of an array: you have to move all the data after the deleted bit towards the start, to close the gap(s) between the deleted data and the kept data.

I know you are asking for code but code without understanding these ideas is useless, and these ideas are simple to express in code once you understand them.
Last edited on
Topic archived. No new replies allowed.