我正在尝试编写一个允许用户创建文件夹的程序。用户输入文件夹名称,该输入将插入CreateDirectory函数。但是它不会编译。该文件夹在C:\ test \中创建,其中子文件夹名称是用户输入。有人有什么建议吗?
#include <iostream>
#include <stdlib.h>
#include <windows.h>
using namespace std;
int main()
{
string input = "";
string address = "C:\\test\\";
string total = input+address;
cout << "Please the name of the folder:\n>";
getline(cin, input);
cout << "You entered: " << input << endl << endl;
cout << "Path name is " << address+input << endl << endl;
CreateDirectory(total,NULL);
//system("mkdir " + input); //Same error when I use mkdir
return 0;
}
我从编译器收到此错误。
C:\ C ++ \ file \ main.cpp | 17 |错误:无法转换'std :: __ cxx11 :: string {aka std :: __ cxx11 :: basic_string}'到'LPCSTR {aka const char *}'用于参数'1'到'BOOL CreateDirectoryA(LPCSTR, LPSECURITY_ATTRIBUTES)'|
--- ---更新 感谢您的回复,非常感谢。我使用了c_str()修复。代码现在正在运行。
#include <iostream>
#include <stdlib.h>
#include <windows.h>
using namespace std;
int main()
{
string input1 = "C:\\test\\";
string input2 = "";
cout << "Please enter the name of the folder:\n>";
getline(cin, input2);
string total = input1+input2;
CreateDirectory(total.c_str(),NULL);
return 0;
}