2016-10-18 5 views
0

C++のカレントディレクトリにループを使っていくつかのフォルダを作ってみたい。コードを作成しましたが、次のエラーが発生しました。C++のカレントディレクトリ内に複数のフォルダを作成

cannot convert 'std::string {aka std::basic_string}' to 'const char*' for argument '1' to 'void CreateFolder(const char*)'

私のコードは次のとおりです。

#include<iostream> 
#include<cstdio> 
#include<cstring> 
#include <windows.h> 
#include <cstdio> 
#include<cstdlib> 
#include<fstream> 
#include <sstream> 
using namespace std; 

#define total 28 

std::string to_string(int i) { 
    std::stringstream s; 
    s << i; 
    return s.str(); 
} 


void CreateFolder(const char * path) 
{ 
    if(!CreateDirectory(path ,NULL)) 
    { 
     return; 
    } 
} 


main() 
{ 
    string folder_name; 
    string suffix = ".\\"; // for current directory 

    for(int i=0;i<=total;i++) 
    { 
     folder_name=suffix+to_string(i); 
     CreateFolder(folder_name); 

    } 

} 

は、どのように私は28に... 0,1という名前のこれらのフォルダが作成されますか?

+1

ヒント:文字列または文字リテラルの中でバックスラッシュは何を意味しますか?それについて一瞬考えてみましょう。 –

+0

'CreateFolder(folder_name);'を 'CreateFolder(folder_name.c_str);で置き換え、[' string :: c_str'](http://www.cplusplus.com/reference/string/string/c_str/)を参照してください。方法。 –

+0

この質問のタイトルはまったく間違っています。そして、これはC++とタグ付けされるべきです。それはWINAPIの質問よりもC++の質問のほうが多いです。 –

答えて

0

std::stringを直接char*に渡すことはできません。 c_str()関数を使用すると、生char*std::stringから取り出すことができます。

//std::string -> const char* 
CreateFolder(folder_name.c_str()); 
+0

コンパイル時に次のエラーが発生しました:エラー: 'LPCWSTR {aka const wchar_t *}'を 'LPCSTR {aka const char *}'に '1'を 'BOOL CreateDirectoryA(LPCSTR、LPSECURITY_ATTRIBUTES)'に変換できません@Doncot – user76289

+0

、 ごめんなさい。 Multi-char設定(= UNICODEではない)を使用しているようです。私の答えを修正する。 – Doncot

+1

@Doncot:* "Multi-char設定を使用しています" * - マルチバイト*(MBCSのように)と呼ばれ、実際にはWindowsではDBCSです。それは、あなたが使いたくない**設定**です。本当の修正は、 'std :: wstring'を使って' CreateDirectoryW'を呼び出すことです。 – IInspectable

関連する問題