Renaming temp.txt with Movies.txt

I am a beginner in C++ programming. I am trying to build a coding for moving reservation system. So, in that program I have the option to delete a movie in the text file. I have done the programming to create a temp file to copy all the movie names there so that no blank space will be in the text file. But the temp file is replacing the original file. Only the temp.txt is created properly. I need someone to help me where the temp.txt will be renamed as Movies.txt replacing the original "Movies.txt" file. This is my coding:

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
  blank_line(const char *file_name)
{

ifstream fin(file_name);

ofstream fout;
fout.open("temp.txt", ios::out);

string str;
while(getline(fin,str))
{
while (str.length()== 3 )
getline(fin,str);

fout<<str<<endl;
}
fout.close();
fin.close();
remove(file_name);
rename("temp.txt", file_name);
}

delete_movie()
{
fstream movie;
fstream temp;

movie.open("Movies.txt",ios::in);
temp.open("temp.txt",ios::out);
char name [25];
char time1[25];
char time2[25];
char time3[25];
char a[25];
cin.ignore();
cout << endl;
cout<<"Enter the name of the movie that you want to delete : ";
cin.getline(a,25);
while(!movie.eof())
{
    movie.getline(name,25,'|');
    movie.getline(time1,25,'|');
    movie.getline(time2,25,'|');
    movie.getline(time3,25);
    if(strcmp(name,a)==0)
    {
        continue;
    }
    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,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");
blank_line("Movies.txt");
cout<<"\n done !!! \n";
}
See your previous thread here http://www.cplusplus.com/forum/beginner/275186/
Hello Mathavan,

I started by putting your code into my IDE and adding some blank lines so that I could actually read it and understand it better.

If you are trying to write your code to benefit the compiler then you have to much white space.

Based on what you posted I received 91 errors and 2 warnings when I tried to compile it.

It is best to post a complete program that tan be compiled and run for testing. Also you need enough code to duplicate the problem that you have, which may nt mean that you will need all of the ode, but it helps.

You are mixing C strings and C++ strings in your program. Is there a reason or need for this? Also you are mixing C and C++ code which is not always a good idea.

Your description of the what the program should do is nice, but feels like there is
something missing.

It is best to post the full instructions that you were given, so that everyone knows what needs to be done along with what can and can not be used. Like is there a reason or requirement that you have to work with files. seeplus has offered an idea of reading the file and storing the information in the program as it runs and then writing any changes back to the original file. If this method is not acceptable then you need to say so.

It is hard to tell what is wrong with the code that you posted because I can not debug the code with all the errors due to the missing parts.

One part I did notice is that your while loops will not work the way that you are thinking. You are checking for (eof) before you actually read and set the (eof) bit. The end results may depend on the C++ standard the you are using with the compiler. This may produce a duplicate last read or a blank record.

The "remove" and "rename" appear to be correct for now, but you should have a look at:
http://www.cplusplus.com/reference/cstdio/remove/
http://www.cplusplus.com/reference/cstdio/rename/

The example code shows to check the return value of the functions and this is something that you should be doing. Just like when you open a file stream you should check it to make sure it worked.

Andy
Hello Mathavan,

I did work up something to test the function and find some potential problems.

To start with:

        1 file(s) copied.

Enter the name of the movie that you want to delete:


Ignore the first line for now. I will explain it later.

When given this prompt what movie name do you enter? Does it come from a piece of paper with the movie name(s) to work from or does the user have to guess?

It would be nice if you could list the movie names that are in the file.

The next problem is let us say that "Batman" is the movie to delete, but the user enters "batman". To us they would be considered equal, but to the computer and a compare function they are different because the "B" and "b" are 2 different letters with 2 different ASCII values.

This is something that you need to account for and change if necessary. The function you could use is "std::toupper()" from the "<cctype>" header file.

If you use the while loop correctly you will only need 1.

At the end of the function the "remove" and "rename" work fine.

And if you while loop is correct there is no need for the "blank_line" function because there will be nothing at the end of the "temp" file to deal with.

Given the original file:

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_



The function would produce:

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


Other than the removed movie name the only difference between the 2 files is that the original file ends after the (0) of the 4th line and in the shortened file it ends on the next line. The (_) is to show where the file ends. When reading the file it makes no difference where the (_) ends up.

This is what I did with "main":
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
    system("copy \"Orig Movies.txt\" Movies.txt"); // <--- Used for testing. Remove when finished.

    int result{};

    if (result = delete_movie())
    {
        return result;
    }

    return 0;
}

Line 3 will restore "Movies.txt" to its original state before the program runs.

Since the "delete_movie()" renames "temp" to "Movies.txt" it is hard to use the file for testing when it keeps getting shorter.

The "delete_movie()" function can be made to work and you can eliminate the need for the "blank_line()" function.

In the end this is still not the best way to deal with this as seeplus has pointed out.

Andy
Topic archived. No new replies allowed.