How to delete and edit record to the data

Hello I am beginner in C++. I programmed a code for deleting and editing time of movies in text file. But I have stuck there. The program is deleting all the data in the file. Can someone help me with this? I have attached the arrangement of movie just like how I arranged in text file which is "Movies.txt"

Avenger|09:30|08:00|07:00
Policeman|09:30|08:00|07:00
Superman|09:30|08:00|07:00
Batman|09:30|08:00|07:00

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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
 delete_movie()
{
    char name [25];
    char name_temp [25];
    char time1[25];
    char time2[25];
    char time3[25];
    char time1_temp[25];
    char time2_temp[25];
    char time3_temp[25];
    int response_delete;
    fstream movie;
    movie.open("Movies.txt",ios::in);

    cout<<"======================================================================================================================" << endl;
    cout<<"|SERIAL NUMBER|               TIME[SHOW1]              TIME[SHOW2]              TIME[SHOW3]            |MOVIE NAME|"    << endl;
    cout<<"======================================================================================================================" << endl;

    int count = 0;
    string line;

    ifstream file("Movies.txt");
    while (getline(file, line))
        count++;

    int i = 0;
    while(!movie.eof(), i++ , i<= count)
    {
    movie.getline(name,25,'|');
    movie.getline(time1,25,'|');
    movie.getline(time2,25,'|');
    movie.getline(time3,25);
    cout<<"    (" << i << ") " << "\t\t\t " << time1 << "\t\t\t " << time2 << "\t\t\t " << time3 << "\t\t\t " <<name << "\n";
    }

    ofstream temp;
    temp.open("temp.txt",ios::out);
    cout << endl;
    cout<<"Enter the serial number of movie you want to delete:  ";
    cin>> response_delete;

    int a = 0;
    while(!movie.eof(), a++ , a <= response_delete)
    {
      movie.getline(name,25,'|');
      movie.getline(time1,25,'|');
      movie.getline(time2,25,'|');
      movie.getline(time3,25);
    }

      while(!movie.eof())
    {
      movie.getline(name_temp,25,'|');
      movie.getline(time1_temp,25,'|');
      movie.getline(time2_temp,25,'|');
      movie.getline(time3_temp,25);
      if(strcmp(name_temp,name)==0)
        {
         continue;
        }
        else
        {
         temp<< name_temp <<'|'<< time1_temp <<'|'<< time2_temp <<'|' <<time3_temp <<'\n';
        }
    }

      while(!movie.eof())
    {
      movie.getline(name_temp,25,'|');
      movie.getline(time1_temp,25,'|');
      movie.getline(time2_temp,25,'|');
      movie.getline(time3_temp,25);
        if(strcmp(name_temp,name)== 0)
        {
            continue;
        }
        else
        {
            temp << name_temp <<'|'<<time1_temp<<'|'<<time2_temp<<'|'<<time3_temp<<'\n';
        }
    }


}
update_movie()
{
    char name [25];
    char name_temp [25];
    char time1[25];
    char time2[25];
    char time3[25];
    char time1_temp[25];
    char time2_temp[25];
    char time3_temp[25];

    int response_update;
    fstream movie;
    movie.open("Movies.txt",ios::in);

    cout<<"======================================================================================================================" << endl;
    cout<<"|SERIAL NUMBER|               TIME[SHOW1]              TIME[SHOW2]              TIME[SHOW3]            |MOVIE NAME|" << endl;
    cout<<"======================================================================================================================" << endl;


    int count = 0;
    string line;

    ifstream file("Movies.txt");
    while (getline(file, line))
        count++;
    int i = 0;
    while(!movie.eof(), i++ , i<= count)
    {
    movie.getline(name,25,'|');
    movie.getline(time1,25,'|');
    movie.getline(time2,25,'|');
    movie.getline(time3,25);
    cout<<"    (" << i << ") " << "\t\t\t " << time1 << "\t\t\t " << time2 << "\t\t\t " << time3 << "\t\t\t " <<name << "\n";
    }

    fstream temp;

    movie.open("Movies.txt",ios::in);
    temp.open("temp.txt",ios::out);
    cin.ignore();
    cout<<"Enter the serial number of movie you want to update:  ";
    cin>> response_update;

    int a = 0;
    while(!movie.eof(), a++ , a <= response_update)
    {
      movie.getline(name,25,'|');
      movie.getline(time1,25,'|');
      movie.getline(time2,25,'|');
      movie.getline(time3,25);
    }

    while(!movie.eof())
    {
      movie.getline(name_temp,25,'|');
      movie.getline(time1_temp,25,'|');
      movie.getline(time2_temp,25,'|');
      movie.getline(time3_temp,25);
        if(strcmp(name,name_temp)==0)
        {
            cout<<"Enter the new time for SHOW[1] : ";
            cin.getline(time1,25);
            cout<<"Enter the new time for SHOW[2] : ";
            cin.getline(time2,25);
            cout<<"Enter the new time for SHOW[3] : ";
            cin.getline(time3,25);
            temp << name<<'|'<<time1<<'|'<<time2<<'|'<<time3<<'\n';
        }
        else
        {
            temp << name<<'|'<<time1<<'|'<<time2<<'|'<<time3<<'\n';
        }

    }
    temp.close();
    movie.close();

    movie.open("Movies.txt",ios::out);
    temp.open("temp.txt",ios::in);
    while(!temp.eof())
    {
        temp.getline(name_temp,25,'|');
        temp.getline(time1,25,'|');
        temp.getline(time2,25,'|');
        temp.getline(time3,25);
        movie << name<<'|'<<time1<<'|'<<time2<<'|'<<time3<<'\n';
    }
    temp.close();
    movie.close();
    remove("temp.txt");
    cout<<"\n done !!! \n";
}
Last edited on
At lines 27-34 you read movie to the end of the file. Then at lines 43-49 you try to read it again. Since you've already read to the end of the file, nothing gets read at all.

Add movie.seekg(0); before line 43.

Some other comments:

Lines 67-81 appear to be a duplicate of lines 51-65.

You might want to learn about structures next. Storing the movie data in a structure will simplify writing a functions to read and write the data for a single movie. Those functions will greatly simplify the code.
Yeah, all the concepts of structure (both data and code) fell on deaf ears.
https://www.cplusplus.com/forum/beginner/275179/
When dealing with changes to a file, unless there's reasons why (file too large etc), the easiest way to proceed is to read the whole file into say a vector, then make the required changes etc and then re-write the whole file from the vector.

Consider which reads the file, allows the contents to be displayed, a film to be deleted and the file re-written.

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
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

const std::string mvfn {"movies1.txt"};

struct Movie1 {
	std::string name;
	std::vector<std::string> times;
};

std::istream& operator>>(std::istream& is, Movie1& mv1)
{
	std::string line;
	std::getline(is, line);

	std::istringstream iss(line);

	std::getline(iss, mv1.name, '|');
	mv1.times.clear();
	for (std::string tm; std::getline(iss, tm, '|'); mv1.times.push_back(tm));

	return is;
}

std::ostream& operator<<(std::ostream& os, const Movie1& mv1)
{
	os << mv1.name << "  ";
	for (const auto& t : mv1.times)
		os << t << " ";

	return os;
}

void display_all(const std::vector<Movie1>& mv1)
{
	std::cout << '\n';
	for (size_t i {}; const auto m : mv1)
		std::cout << '(' << ++i << ") " << m << '\n';
}

auto read1()
{
	std::vector<Movie1> mvs;
	std::ifstream ifs(mvfn);

	for (Movie1 mv1; ifs >> mv1; mvs.push_back(mv1));

	return mvs;
}

bool write1(const std::vector<Movie1>& mvs)
{
	std::ofstream ofs(mvfn);

	if (!ofs)
		return false;

	for (const auto& [nam, times] : mvs) {
		ofs << nam;

		for (const auto& t : times)
			ofs << '|' << t;

		ofs << '\n';
	}

	return true;
}

bool erase(std::vector<Movie1>& mvs)
{
	std::cout << "\nAvailable movies are:\n";
	display_all(mvs);

	size_t selection_movie {};

	std::cout << "\nPlease select a movie : ";
	std::cin >> selection_movie;

	if (selection_movie >= 1 && selection_movie <= mvs.size()) {
		mvs.erase(mvs.begin() + selection_movie - 1);
		return true;
	}

	std::cout << "Invalid movie number\n";
	return false;
}

int main()
{
	auto mvs {read1()};

	if (mvs.empty())
		return (std::cout << "Problem reading data\n"), 1;

	do {
		std::cout << "\n (1) Display Movies\n";
		std::cout << " (2) Delete Movie\n";
		std::cout << " (3) Change a time\n";
		std::cout << " (4) Delete a time\n";
		std::cout << " (0) Exit (and update file)\n\n";
		std::cout << " Please specify the selection: ";

		int selection_role {};
		std::cin >> selection_role;

		switch (selection_role) {
			case 1:
				display_all(mvs);
				break;

			case 2:
				erase(mvs);
				break;

			case 3:
				std::cout << "(To do)\n";
				break;

			case 4:
				std::cout << "(To do)\n";
				break;

			case 0:
				std::cout << (write1(mvs) ? "Write OK\n" : "Problem writing file\n");
				return 0;

			default:
				std::cout << "Invalid option\n";
				break;
		}
	} while (true);
}

Last edited on
A basic frame could be:
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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <exception>
#include <sstream>

struct Movie
{
   std::string name;
   std::vector<std::string> times;
   Movie(): times(3) {}
};

// Both functions need to be adjusted by your needs!
std::istream & operator>>( std::istream & is, Movie & movie )
{
    is >> movie.name;
    for( std::string & time : movie.times )
        is >> time;
    return is;
}
std::ostream & operator<<( std::ostream & os, const Movie & movie )
{
    os << movie.name << ' ';
    for( const std::string & time : movie.times )
        os << time << ' ';
    return os;
}

int read_integer( std::istream & is )
{
    int val;
    while( true )
    {
        std::string tmp;
        std::getline( is, tmp);
        try { val = std::stoi( tmp ); }
        catch (...) { continue; }
        break;
    }
    return val;
}

Movie read_movie( std::istream & ifile )
{
    // tweak it by yourself...
    Movie movie;

    if( ifile )
        ifile >> movie;
    else
        throw std::ios_base::failure(
            "read_movie(): couldn't read from ifile.\n"
        );
    return movie;
}

void show_movie_list( const std::vector<Movie> & movie_list )
{
    // tweak it by yourself...
    for( std::size_t i = 0; i < movie_list.size(); ++i)
    {
        std::cout << i+1 << ": " <<  movie_list[i] << '\n'; // tweak it by yourself
    }
}
std::vector<Movie> read_movie_list( std::istream & ifile )
{
    std::vector<Movie> movie_list;
    while( true )
    {
        Movie movie;
        ifile >> movie;
        if( ifile )
        {
            movie_list.push_back( movie );
        }
        else
            break;
    }
    return movie_list;
}

void write_movie_list( std::ostream & ofile, const std::vector<Movie> & movie_list )
{
    for( const auto & movie : movie_list )
    {
        if( ofile )
            ofile << movie << '\n';
        else
            throw std::ios_base::failure(
                "write_movie_list(): couldn't write to ofile.\n"
             );
    }
}

void delete_movie( std::vector<Movie> & movies )
{

    std::cout << "Which movie-No. do you want delete?";
    int del_no = read_integer( std::cin );
    -- del_no; // because zero-based index;
    if( del_no >= 0 && del_no < movies.size() )
        movies.erase( movies.begin() + del_no );
}

void update_movie( std::vector<Movie> & movies )
{
    std::cout << "Which movie-No. do you want update?";
    int update_no = read_integer( std::cin );
    -- update_no; // because zero-based index;
    if( update_no >= 0 && update_no < movies.size() )
        movies[update_no] = read_movie( std::cin ); // tweak it by your own
}

void test()
{

    // TODO: replace the stringstreams with your files...
    std::istringstream movies_old_file(
      std::string( "Avenger 09:30 08:00 07:00\n" )
    + "Policeman 09:30 08:00 07:00\n"
    + "Superman 09:30 08:00 07:00\n"
    + "Batman 09:30 08:00 07:00\n"
    );
    std::stringstream movies_new_file;

    std::vector<Movie> movies = read_movie_list( movies_old_file);
    show_movie_list( movies );
    update_movie( movies );
    show_movie_list( movies );
    delete_movie( movies );
    write_movie_list( movies_new_file, movies );

    // assure that the new file was correctly written
    movies = read_movie_list( movies_new_file );
    show_movie_list( movies );

}

int main()
{
    test();
}
Sorry @seeplus, I have overlooked that you've posted in the meantime a proper solution.
Topic archived. No new replies allowed.