fstream remove() function isn't deleting this file

Jun 19, 2018 at 5:01pm
Hi, I'm trying to make a puzzle game in Dev-C++, and one of the solutions is manually renaming a text file to 'cascatallus.txt' then telling the program to read it. It is reading the file and is getting to the next level, but I wanted it to delete 'cascatallus.txt', so I added a remove() function, which has worked before in the same program, but it isn't deleting this file for some reason. I'd like to know what I can do to get it to work.

I'm sending the source code for this level and also for the level where the remove() function worked.

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
#include<iostream>
#include<stdlib.h>
#include<fstream>

using namespace std;

main()
{
	ofstream file;
	string a;
	cout << "\n\t Enter command:";
	cout << "\n\t "; getline(cin, a);
	if(a == "start")
	{
		file.open ("file.txt");
		file << "bubbles";
	}
	else if(a == "181")
	{
		remove ("file.txt");
		file.open ("fjie.txt");
		file << "\"file.txt\" not found";
		cout << "\n\t 404";
		system("pause>>null");
	}
	else if(a == "fjie")
	{
		remove ("fjie.txt");
		file.open ("file.txt");
		file << "hint hint hint hint";
		cout << "\n\t File.txt has a friend.";
		system("pause>>null");
	}
	else if(a == "read cascatallus")
	{
		ifstream read("cascatallus.txt");
		if(read)
		{
			remove ("cascatallus.txt");
			file.open ("file.txt");
			file << "Kl. Gr brx uhphpehu ph? Wbsh pb qdph dv d frppdqg.";
			cout << "\n\t Looks like you have a pen pal.";
			system("pause>>null");
		}
		else
		{
			cout << "\n\t 404";
			system("pause>>null");
		}
	}


Any help is welcome.
Last edited on Jun 19, 2018 at 5:22pm
Jun 19, 2018 at 5:07pm
Why are you opening the file if you are trying to delete it? That's probably what's stopping it from being removed.

Anyway, if remove fails it will return non-zero, at which point you can call perror("remove failed") to print the error message.
Last edited on Jun 19, 2018 at 5:20pm
Topic archived. No new replies allowed.