2016-08-02 62 views
0
system("mkdir C:\\Users\\USER\\Desktop\\test"); 

,我發現這個然而,這並不工作,因爲我的代碼看起來像這樣用戶自定義的C++系統功能目錄執行從

string inputFAT = "input.FAT"; 
    string outputdirecotry = "adirectory"; 
    string exepath = "FATool.exe"; 

    cout << "enter the directory you would like to have the files put out to"; 
    getline(cin, outputdirecotry); 
    string outputdirectorycommand = "cd " + outputdirecotry; 


    cout << "enter the path of the file you want to extract"; 
    getline(cin, inputFAT); 

    cout << "enter the path to the FATool executable"; 
    getline(cin, exepath); 

    string exportcommand = exepath + " -x " + inputFAT; 
    system(outputdirectorycommand.c_str && exportcommand.c_str()); 

,你可以看到我需要的用戶定義的目錄該系統功能需要去,當我試圖建立它,它拋出這些錯誤

嚴重性代碼說明項目文件的線路抑制狀態 錯誤C3867「的std :: basic_string的,性病::分配器> :: c_str':非標準語法;使用 '&' 創建一個指向成員FATool ++ C:\用戶\拉斯\文件\的Visual Studio 2015年\項目\ fatool ++ \ fatool ++ \ main.cpp中24

而且這個

嚴重性代碼說明項目文件行抑制狀態 錯誤C2664'int system(const char *)':無法將參數1從'bool'轉換爲'const char *'FATool ++ c:\ users \ russ \ documents \ visual studio 2015 \ projects \ fatool ++ \ fatool ++ \ main.cpp 24

所以它甚至有可能做到這一點,或者我應該只是把我的損失,並定義目錄我和有我的朋友們進入代碼和做同樣的事情

回答

1

傳遞給system()參數錯誤:

system(outputdirectorycommand.c_str && exportcommand.c_str()); 

語法outputdirectorycommand.c_str是錯誤的,並且傳遞給system()的參數是bool,這顯然是錯誤的。

假設你想要做的就是執行cd <x> && FATool.exe -x <xxx>,那麼你應該cat你的命令,並把它傳遞給system()

string cmdToExecute = outputdirectorycommand + " && " + exportcommand; 
system(cmdToExecute.c_str()); 
+0

哇好感謝我從來沒有教過這樣做,它現在似乎很明顯 –

1
system(outputdirectorycommand.c_str && exportcommand.c_str()); 

這將嘗試採取性病的地址::字符串: :c_str函數,將其轉換爲布爾型和邏輯型,並使用exportcommamd.c_str()的返回值的布爾轉換對其進行測試。

你可能打算

system(outputdirectorycommand.c_str() + " && " + exportcommand.c_str()); 
+0

我想,但是我認爲,第一個答案是阻力最小的路徑仍值得的給予好評的作品太雖然 –