【问题标题】:Problem with remove function while trying to remove a file from directory in C++尝试从 C++ 中的目录中删除文件时删除函数出现问题
【发布时间】:2022-07-20 17:50:50
【问题描述】:

我需要根据用户的输入从目录中删除文件并将其传递给执行文件删除程序的函数

/* 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";
  }
}

好的,现在我在remove 函数上遇到了几个错误:function "remove" cannot be called with the given argument list。我不确定这个错误是什么意思,所以我想解释一下。

【问题讨论】:

  • 附带说明,stdio.h 和 cstdio 是同一个东西,强烈建议在 C++ 中使用 cstdio 而不是 studio.h。

标签: c++


【解决方案1】:

remove 采用 C 字符串,但您的表达式 "C:\\MAIN_LOC\\" + file + ".txt" 是 C++ 字符串。使用c_str方法转换为C字符串

remove(("C:\\MAIN_LOC\\" + file + ".txt").c_str())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-08
    • 2010-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多