Deleting files PLEASE HELP!

I am trying to teach myself file I/O by making a address book program where the user can create, edit, and list all of their contacts. The issue I am having is that I want them to be able to delete a contact, i can't use the 'remove()' function because it only takes const char's. The issue is in the second switch statement. Here is my code:

This is just the edit function.

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
system("cls");
	string line;

	cout << " _-=*******************************AdressBook*******************************=-_" << endl;
	cout << "|                                                                              |" << endl;
	cout << "|                                                                              |" << endl;
	cout << "|                                                                              |" << endl;
	cout << "|                            *Enter the name of the                            |" << endl;
	cout << "|                                                                              |" << endl;
	cout << "|                            contact you would like                            |" << endl;
	cout << "|                                                                              |" << endl;
	cout << "|                                   to edit*                                   |" << endl;
	cout << "|                                                                              |" << endl;
	cout << "|                                                                              |" << endl;
	cout << "|______________________________________________________________________________|" << endl << endl;
	cout << "Name of the contact: ";
	cin >> Person.Name;

	ifstream file ("Contacts\\" + Person.Name + ".cnt");
	
	while(!file.eof()){

		for(int i = 0; i < 4; i++){
		
			getline(file, line);
			if(i == 0){
				Person.Name = line;
			}else if(i == 1){
				Person.CellNumber = line;
			}else if(i == 2){
				Person.PhoneNumber = line;
			}else if(i == 3){
				Person.StreetAdress = line;
			}

		}		

		break;

	}

	fs.close();

	cout << "_--=*******************************AdressBook*******************************=--_" << endl;
	cout << "|                                                                              |" << endl;
	cout << "|                                 Edit Contact                                 |" << endl;
	cout << "|                                                                              |" << endl;
	cout << "|                                                                              |" << endl;
	cout << "                     Name: " << Person.Name << endl;
	cout << "|                                                                              |" << endl;
	cout << "               Cell Phone: " << Person.CellNumber << endl;
	cout << "|              Home Phone: " << Person.PhoneNumber << endl;
	cout << "|                                                                              |" << endl;
	cout << "|                  Adress: " << Person.StreetAdress << endl;
	cout << "|                                                                              |" << endl;
	cout << "|                                                                              |" << endl;
	cout << "|('S' to save, 'M' for main menu)______________________________________________|" << endl;
	cout << "What do you want to do(N/C/H/A or 'D' to delete): ";
	cin >> MenuDecision;

	switch(MenuDecision){
	
		case 'n':
		case 'N':
			cout << "Enter the persons name (use '_' instead of spaces): ";
			cin >> Person.Name;
			Create();
			break;

		case 'c':
		case 'C':
			cout << "Enter their cell phone number (use '-' inbetween numbers): ";
			cin >> Person.CellNumber;
			Create();
			break;

		case 'h':
		case 'H':
			cout << "Enter their home phone number (use '-' inbetween numbers): ";
			cin >> Person.PhoneNumber;
			Create();
			break;

		case 'a':
		case 'A': 
			cout << "Enter their full adress (use '_' instead of spaces): ";
			cin >> Person.StreetAdress;
			Create();
			break;

		case 's':
		case 'S':
			if(Person.Name != ""){
				fs.open("Contacts\\" + Person.Name + ".cnt", fstream::trunc + fstream::out);
				fs << Person.Name << endl;
				fs << Person.CellNumber << endl;
				fs << Person.PhoneNumber << endl;
				fs << Person.StreetAdress << endl;
				fs.close();
				main();
			}else{
				Create();
			}
			break;

		case 'd':
		case 'D':
			remove("Contacts\\" + Person.Name + ".cnt");
			main();

		case 'm':
		case 'M':
			main();
			break;

		default:
			Create();

	}

	return 0;

}


Try to keep it simple because I am kind of a newbie. If it has to be complicated try explaining it to me.

Thank you.
If you wrote that function, why not just change it to take a string? If not, just pass ("Contacts\\" + Person.Name + ".cnt").c_str()
no remove isn't my own function, it deletes files. look up the syntax for remove for c++. it says it takes const char's. And i tried the the .c_str, and that didn't delete the file. It didn't do anything actually.

EDIT: Okay the .c_str() thing worked its just that the destination was wrong but thank you so much!
Last edited on
Check and see if the remove failed and why.
Example here: http://www.cplusplus.com/reference/cstdio/remove/
Okay it actually doesn't still work and this is getting very frustrating. The remove thing only works if there isn't and variable involved and when i do add in Person.Name it stops working.
I get it now! I was trying to delete the file while it was still in use. Thats why it wasn't deleting thanks so much for helping me out. And sorry that I have been a bit confusing.
Topic archived. No new replies allowed.