【问题标题】:How do I make a C++ program that opens another program? [duplicate]如何制作打开另一个程序的 C++ 程序? [复制]
【发布时间】:2015-12-24 18:21:36
【问题描述】:

所以我知道怎么做,我只是想让它打开文件而不指定路径。

例如:我在里面

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;
}

我怎样才能让它在任何人的电脑上运行,不管他们把它放在哪里? 另外,如果您可以重写我的代码作为示例,我将不胜感激。

【问题讨论】:

  • 我认为cout &lt;&lt; " 1) BSCV2 &lt;&lt; endl; 不会编译,是吗?
  • 我一定是不小心删除了最后的“。我的意思是cout &lt;&lt; " 1) BSCV2" &lt;&lt; endl;
  • 当您从 IDE 逐字复制/粘贴测试用例时,“意外删除”字符令人印象深刻。
  • 我实际上删掉了一些代码,因为它是相同的,但会(至少应该)打开另一个可执行文件。

标签: c++


【解决方案1】:

您需要获取当前登录用户的“主”目录。参考这篇文章: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)?

你最好写一个函数来搜索可执行文件,或者让用户指定它在哪里。

【讨论】:

    猜你喜欢
    • 2018-10-13
    • 2018-11-19
    • 2010-11-05
    • 2015-07-22
    • 2013-07-20
    • 1970-01-01
    • 1970-01-01
    • 2012-02-19
    • 2018-11-09
    相关资源
    最近更新 更多