2012-05-07 5 views
-1

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

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

ような何かを行うときに私のコンパイラは、「オペレータ< <なしのマッチ 『C:< < NEWNAME \』」私にエラーを与える

これは私のコードです。問題は、void newgame()です。

#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 

答えて

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

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

私はしかし、疑い、CreateDirectoryはC文字列ではなく、std::stringを取ることを、あなたは.c_str()メンバーとそれを変換する必要がありますので。

+1

のWin32 APIのparamsは、一般的にLPCSTRまたはLPSTRです。コンパイルオプション(Unicodeかどうか)によっては、これは(const)CHAR *またはWCHAR *へのポインタです。 –

0

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

std::ostringstream buffer; 

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

CreateDirectory(buffer.str().c_str()); 
関連する問題