-1

文字列変数をCreateDirectoryに挿入する方法はありますか?ユーザーが入力した名前でC:にディレクトリを作成したいと思います。私が何かをするとき

CreateDirectory ("C:\\" << newname, NULL); 

私のコンパイラは、「'C:\<<newname'のoperator<<に一致しません」というエラーを表示します。

これは私のコードです。問題はvoidnewgame()にあります。

#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
4

2 に答える 2

3
CreateDirectory (("C:\\" + newname).c_str(), NULL);

std::stringとを結合できますoperator+。または、あなたの場合、C 文字列をstd::stringusingoperator+に結合することもできます。結果はstd::string. (ただし注意してください -- 2 つの C 文字列をそのように結合することはできません。)

ただし、これCreateDirectoryは ではなく C 文字列を取ると思われるため、メンバーstd::stringで変換する必要があります。.c_str()

于 2012-05-07T15:11:33.153 に答える
0

ストリーム挿入を使用するには、最初にストリームを作成する必要があります。

std::ostringstream buffer;

buffer << "c:\\" << newname;

CreateDirectory(buffer.str().c_str());
于 2012-05-07T15:12:29.453 に答える