Getting Error when using remove() with strings

Dec 2, 2015 at 9:01am
I'm trying to make it so that the user of a program can delete a file depending on the number the enter. If the user enters 1, the code would check if "file1.txt" exists and then delete it. However, I'm getting an error "'int remove(const char *)': cannot convert argument 1 from 'std::string' to 'const char *'". I'm confused as to how I can make it so remove() will work with strings. Help is appreciated!

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
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;

void deleteQuiz(){
	ifstream inputFile;
	int	 num;			
	string   testFile,
		 txtExt = ".txt", // Text Extension
		 file = "file";

	stringstream sstm1;

	cout << "Enter the number of the file you would like to delete: ";
	cin >> num;

                // Concatenating a string, int, and string together.
		sstm1 << file <<  num << txtExt;
		testFile = sstm1.str();

		inputFile.open(testFile);

		// Tests if file exists.
		if (inputFile){
			remove(testFile);
			cout << "\n<Files successfully deleted.>\n";
		}
		else{
			cout << "\n<Error removing file.>\n";
		}
}
Last edited on Dec 2, 2015 at 9:21am
Dec 2, 2015 at 9:05am
What coder777 said
Last edited on Dec 2, 2015 at 9:07am
Dec 2, 2015 at 9:05am
remove(testFile.c_str());
Dec 2, 2015 at 9:20am
Thanks for responding. I replaced that bit of code with the one coder777 suggested but I'm still getting a build error for some reason.

Edit: Nevermind. I'm an idiot. Forgot to close an another open .cpp file... Thank you for the help!
Last edited on Dec 2, 2015 at 9:26am
Topic archived. No new replies allowed.