在我的c++程序中打开Microsoft Word

Opening up Microsoft Word in my C++ program

本文关键字:Microsoft Word 我的 c++ 程序      更新时间:2023-10-16

正如标题所提到的,我正在尝试通过这个程序打开Microsoft Word,并且遇到了一点困难。在对进程进行了一些研究之后,我决定使用进程ID和Fork函数来处理打开程序中的另一个文件。我似乎遇到一些困难的领域是与执行家庭功能。这些函数似乎有各种不同的用途,但我很难理解应该使用哪个函数,以及我是否在语法上正确地布局了我的论点。

当我输入"msword"时,我的控制台在屏幕上打印出以下内容:

你好——,你想打开什么应用程序?

msword

创建子进程打开Microsoft Word

父进程

打开Microsoft Word

#include <stdio.h>
#include <iostream>
#include <string>
// Routine Headers
#include <sys/types.h>
#include <unistd.h>
using namespace std;
//function that actually processes loading the program, will take the result of searchApplication
void loadApplication(string path)
{
    // If the user typs Microsoft Word (msword abbreviation...)
    if(path == "msword")
    {
        cout << "Creating Child Process To Open Microsoft Wordn";
        pid_t ProcessID = fork();
        if(ProcessID == -1)
        {
        cout << "Error creating another Process... Exitingn";
        exit(1);
        }
        // This is the child process
        else if (ProcessID == 0)
        {
            execle("/Applications/Microsoft Office 2011", nullptr);
        }
        // This is the parent process
        else
        {
            cout << "parent processn";
        }
    }
int main()
{
    cout << "Hello ---, what application would you like to open?n";
    string input;
    cin >> input;
    loadApplication(input);

    return 0;
}

您不必为此使用fork/exec。将open命令传递给system():

#include <cstdlib>
int main() {
   system("open /Applications/App\ Store.app");
   return 0;
}

请注意,您需要转义应用程序名称中的任何空格(如上所示),并指定全名(而不仅仅是显示的名称)。

这里有一个非常相关的问题