【问题标题】:System Error C++ [duplicate]系统错误 C++ [重复]
【发布时间】:2017-03-21 05:29:53
【问题描述】:

所以我正在制作一个脚本来制作一个 txt 文件并将数据放在上面,但是当我运行 system();函数,我得到一个奇怪的错误。这是代码。

#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;

int main()
{
    string textFileName = "SavedPasswords.txt";
    string currentPassword = "Pas123!()";
    string currentName = "Home";
    string seperator = "====================";

    ofstream textFile(textFileName.c_str());

    textFile << "N: " << currentName << endl << "P: " << currentPassword << endl << endl << seperator << endl;

    string directory;
    size_t path = textFileName.rfind("\\");

    if(string::npos != path)
    {
        directory = textFileName.substr(0, path);
    }

    string systemCommands[3] = {"cd\\",
    "cd " + directory,
    "start " + textFileName};

    system(systemCommands[0]);
    system(systemCommands[1]);
    system(systemCommands[2]);
}

C:\Users\User\Desktop\SavePasswords\main.cpp|29|error: cannot convert 'std::string {aka std::basic_string}' to 'const char*' for argument '1' to ' int 系统(const char*)'|

接下来的两行出现同样的错误。

【问题讨论】:

  • 你试过system(systemCommands[0].c_str()); ?
  • 更喜欢&lt;cstdlib&gt; 而不是&lt;stdlib.h&gt;
  • 只需谷歌搜索错误字符串即可立即为您提供答案

标签: c++


【解决方案1】:

std::system ()

只接受 'char *' 类型的字符串,这与 'std::string' 不同。尝试使用 'systemCommands[0].c_str()' 这个函数成员从 'std::string' 内部获取 'const char *' 数据。检查this

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-17
    • 2013-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-25
    相关资源
    最近更新 更多