【发布时间】: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()); ?
-
更喜欢
<cstdlib>而不是<stdlib.h> -
只需谷歌搜索错误字符串即可立即为您提供答案
标签: c++