2017-03-21 2 views
-3

私はtxtファイルを作成してデータを置くスクリプトを作っていますが、system();関数、私は奇妙なエラーが発生します。ここにコードがあります。システムエラーC++

#include <iostream> 
#include <fstream> 
#include <stdlib.h> 
using namespace std; 

int main() 
{ 
    string textFileName = "SavedPasswords.txt"; 
    string currentPassword = "Pas123!()"; 
    string currentName = "Home"; 
    string seperator = "===================="; 

    ofstream textFile(textFileName.c_str()); 

    textFile << "N: " << currentName << endl << "P: " << currentPassword << endl << endl << seperator << endl; 

    string directory; 
    size_t path = textFileName.rfind("\\"); 

    if(string::npos != path) 
    { 
     directory = textFileName.substr(0, path); 
    } 

    string systemCommands[3] = {"cd\\", 
    "cd " + directory, 
    "start " + textFileName}; 

    system(systemCommands[0]); 
    system(systemCommands[1]); 
    system(systemCommands[2]); 
} 

C:\ Users \ユーザーデスクトップ\ユーザー\ SavePasswords main.cppに\ | 29 |エラー:引数は 'のconstのchar *' に '{のstd ::のbasic_string別名}のstd :: string' を変換することはできません " 1 'から' int system(const char *) 'に変更します。

次の2行で同じエラーが発生します。

+3

システム(systemCommands [0] .c_str())を試したことがありますか。 ? – JGroven

+0

は、「」から「」 –

+0

を選択すると、エラー文字列がグーグルグーグルで返されます –

答えて

2

std::system()

は、 'std :: string'とは異なる 'char *'タイプの文字列のみを受け入れます。 'systemCommands [0] .c_str()'を使用してください。この関数メンバーは、 'std :: string'内部から 'const char *'データを取得します。 thisを確認してください。

関連する問題