Removing and renaming a file

I am creating a program which allows a user to enter and store various data about a pet. Case 3 should allow the user to delete a pet from the file, however the code I have compiles but does not delete any data from my "pet.txt file. Attached is the full code but case 3 is where my issues are. How do I properly remove and rename this file with my program?

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
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cmath>

using namespace std;

int main()

{
    int user_choice;
    int page;
    string pname;
    string ptype;
    string pcomment;
    string myText;
    string find;
    string line;

    cout << "\nManage Your Pet\n" <<
        "  1. Create Pet\n" <<
        "  2. Display Pet\n" <<
        "  3. Delete Pet\n" <<
        "  4. Exit\n" <<
        "\nEnter your choice (1-4): ";
    cin >> user_choice;
    cout << endl;

    switch (user_choice) {
    case 1: {

        ofstream fw("pet.txt", std::ofstream::out);

        if (fw.is_open()) {
            cout << "What is your pet's name?" << endl;
            cin >> pname;
            fw << pname << '\n';
            cout << "What is your pet's type?" << endl;
            cin >> ptype;
            fw << ptype << '\n';
            cout << "How old is your pet?" << endl;
            cin >> page;
            fw << page << '\n';
            cout << "Enter a comment about the pet!" << endl;
            cin >> pcomment;
            fw << pcomment << '\n';
            cout << "Data stored!" << endl;
            fw.close();
        }
        else cout << "Problem with opening file";
        break;
    }

    case 2: {
        ifstream MyReadFile("pet.txt");

        cout << "What is your pet's name?" << endl;
        cin >> pname;

        while (getline(MyReadFile, myText)) {
            cout << myText;
        }

        MyReadFile.close();
        break;
    }

    case 3: {
        cout << "Pet Name? " << endl;
        cin >> find;

        ifstream myfile2;
        myfile2.open("pet.txt");

        ofstream temp;
        temp.open("temp.txt");
        while (getline(myfile2, line)) {
            if (line != find)
                temp << line << endl;
        }
        myfile2.close();
        temp.close();
        remove("pet.txt");
        rename("temp.txt", "pet.txt");
        break;
    }

    case 4: {
        cout << "Good-bye." << endl;
        break;
    }

    default:
        cout << "We're sorry. \nYour choice must " <<
            "between 1 and 4.\n" <<
            "Rerun the program and try again." <<
            endl;
        break;
    }

    cout << endl;

    return 0;
}
Line 78: Your while loop is going to skip over the pet's name, but it's not going to delete the pet's type, age or comment.

Other comments:
Lines 37-46: The use of cin stops at the first whitespace meaning a pet's name or comment can not be more than one word.

the modern c++ way is https://en.cppreference.com/w/cpp/filesystem
the "I can't use that" simple way is operating system commands.
system("del filename"); //filename is the text, not a variable here...
system("ren name name2");
and similar unix commands (rm and probably cp or move? you can look it up)

you can also do c++ to make a new file and open the old one, copy the contents, and then clear the old file but actually removing the now empty file from the OS I do not know.

but if at all possible try to use filesystem.
As jonnin pointed out, the proper way is to use <filesystem> (requires C++17).
If that's not an option for you, here the routines I used before I went to C++17.
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
/  Execute a system command
void Sys_Cmd (const string & cmd)
{
    cout << cmd << endl;
    system (cmd.c_str());
}

//  Remove a file
void Sys_Erase (const string & fname)
{   string      cmd;

    cmd = "ERASE ";
    cmd += fname;
    Sys_Cmd (cmd);
}

//  Rename a file
void Sys_Rename (const string & oldname, const string & newname)
{   string cmd;

    cmd = "RENAME ";
    cmd += oldname;
    cmd += " ";
    cmd += newname;
    Sys_Cmd(cmd);
}

You are only allowing data for 1 pet to be stored. Every time you use option 1 you erase the previous data before saving the new data. Hence option 2) doesn't need to ask for the pet name (which isn't used). Likewise for option 3). You don't ask for the pet name as there's data for only 1 per stored.

option 2) should also check that the file has been opened ok.

So for option 3), all that's needed is to remove all the data from the file. An easy way to do this is simply:

1
2
3
4
5
case 3:
{
    ofstream fw("pet.txt");
}
break;


This will open/create the file, erase any existing contents and then close the file.


Topic archived. No new replies allowed.