我需要根据用户的输入从目录中删除文件并将其传递到执行文件删除程序过程的函数中
/* Class 3 veus 3:45PM*/
#include <string>
#include <iostream>
#include <stdio.h>
#include <cstdio>
void remove_file(std::string file);
int main() {
std::string file_name;
std::cin >> file_name;
remove_file(file_name);
}
void remove_file(std::string file) {
if(remove("C:\\MAIN_LOC\\" + file + ".txt") == 0) {
std::cout << "`" << file << "`" << " Item deleted successfully" << std::endl;
} else {
std::cout << "[Error] File not found";
}
}
Run Code Online (Sandbox Code Playgroud)
好吧,现在的问题是我在remove函数上遇到了几个错误:function "remove" cannot be called with the given argument list。我不确定这个错误是什么意思,所以我想得到解释。
remove接受一个 C 字符串,但你的表达式"C:\\MAIN_LOC\\" + file + ".txt"是一个 C++ 字符串。使用c_str方法转换为C字符串
remove(("C:\\MAIN_LOC\\" + file + ".txt").c_str())
Run Code Online (Sandbox Code Playgroud)