2015-09-27 30 views
0

所以我知道如何製作它,我只是希望它打開文件而不指定路徑。如何製作打開其他程序的C++程序?

例如:我有它在

C:\Users\\(me)\Desktop\Projects\BCs\BSCV2\bin\Debug\BSC.exe 

,但如果我把它送給一個朋友,他有一個不同的用戶名,(him)例如,因此命令將無法即使執行他將它放在桌面上,因爲路徑不再有效。

下面的代碼的一部分:

#include <iostream> 
#include <windows.h> 
using namespace std; 

int main() 
{ 
    int a; 
    cout << endl; 
    cout << " This window is used for launching the programs." << endl; 
    cout << " Type in the number of the program you want to use and press Enter." << endl; 
    cout << endl; 
    cout << " 1) BSCV2 << endl; 
    cout << endl; 
    cout << " "; cin >> a; cout << endl; 
    cout << endl; 

     if (a == 1){ 
      system ("start C:\\Users\\(me)\\Desktop\\Projects\\BCs\\BSCV2\\bin\\Debug\\BSCV2.exe"); 
      system ("pause"); 
     } 

    return 0; 
} 

我怎樣才能使任何人的電腦上運行,而不管它們放在哪裏呢?另外,如果你可以重寫我的代碼作爲例子,我會很感激。

+0

我不認爲'cout <<「1)BSCV2 << endl;'會編譯,會不會? –

+0

我一定是不小心刪除了」最後。我的意思是'cout <<「1)BSCV2 << << endl;' – VeeZee

+0

當您逐字從您的IDE中複製/粘貼測試用例時,意外刪除字符令人印象深刻。 –

回答

0

您將需要獲得登錄的當前用戶的「家」目錄參考這個帖子:How to get the current user's home directory in Windows,或者這一個:How can I find the user's home dir in a cross platform manner, using C++?

但是,你絕對確保所有用戶(在各自的機器)運行你的應用程序將有確切的目錄路徑到你正在嘗試呼叫的可執行文件(\ Desktop \ Projects \ BCs \ BSCV2 \ bin \ Debug \ BSCV2.exe)?

您最好寫一個函數來搜索可執行文件,或者要求用戶指定它的位置。

相關問題