如何将变量插入到创建目录()
How do you insert a variable into CreateDirectory()
有没有办法将字符串变量插入到CreateDirectory中?我希望它用 C: 创建一个目录,其中包含用户输入的名称。当我做类似的事情时
CreateDirectory ("C:\" << newname, NULL);
我的编译器在"C:\"中给了我错误"运算符不匹配<<<<新名字'"
这是我的代码。问题出在无效的新游戏()。
#include <iostream>
#include <fstream>
#include <string>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <mmsystem.h>
#include <conio.h>
using namespace std;
int a;
string newname;
string savepath;
struct game
{
string name;
int checkpoint;
int level;
};
void wait( time_t delay )
{
time_t timer0, timer1;
time( &timer0 );
do {
time( &timer1 );
} while (( timer1 - timer0 ) < delay );
}
void error()
{
cout << "nError, bad input." << endl;
}
void options()
{
cout << "No options are currently implemented." << endl;
}
void load()
{
cout << "Load a Game:n";
}
//This is where I'm talking about.
void newgame()
{
cout << "Name your Game:n";
getline(cin,newname);
cin.get();
game g1;
g1.name=newname;
//I want it to create a dir in C: with the name the user has entered.
//How can I do it?
CreateDirectory ("C:\" << newname, NULL);
}
//This isn't the whole piece of code, just most of it, I can post the rest if needed
CreateDirectory (("C:\" + newname).c_str(), NULL);
您可以将std::string
与operator+
.或者,在您的情况下,您也可以使用 operator+
将 C 字符串连接到std::string
。结果是std::string
.(不过要小心 - 你不能以这种方式将两个 C 字符串连接在一起。
但是,我怀疑CreateDirectory
需要 C 字符串,而不是 std::string
,因此您需要使用 .c_str()
成员对其进行转换。
要使用流插入,您需要首先创建一个流:
std::ostringstream buffer;
buffer << "c:\" << newname;
CreateDirectory(buffer.str().c_str());
相关文章:
- 如何在文档文件夹中创建目录
- 如何在不使用宽字符串的情况下创建目录C++?
- 如何创建目录C (使用_mkdir)
- Qt无法创建目录
- QDir mkpath 返回 true,但未创建目录
- 创建通用插入功能
- 创建目录不创建目录 VS2017 c++
- 为测试单元创建目录 C++ [代码::块]
- 创建目录并导航到目录C
- 我的"out of the box"程序可以在 Windows 中在哪里可靠地创建目录?
- 创建目录中所有.cpp文件和 android.mk 子目录中的FILE_LIST
- 无法访问创建目录创建的目录
- 在C++中创建目录之前检查它是 Windows 还是 Unix
- 在Qt中创建目录的哈希值
- 如何在不使用 boost 的情况下创建目录
- 与长路径一起使用时,创建目录失败并显示错误 123
- 从 QTableWidget 中的目录插入图像
- C或C++或WinApi中是否有任何功能来创建目录,包括指定路径中所有不存在的目录
- 如何将变量插入到创建目录()
- 有没有更好的方法从字符串创建目录?CPP,创建目录,字符串流,字符串