2

As the title mentions, I am trying to open Microsoft Word through this program and am running into a little bit of difficulty. Having done some research into processes, I decided to go through the route of working with Process ID's and the Fork function to handle opening another file within my program. The area where I seem to be running into some difficulty are with the exec family functions. Their seems to be a variety of different uses for these functions, but I am having a difficult time wrapping my head around which function I should use and whether I am syntatically laying out my arguments correctly.

My console prints the following out to the screen when I type "msword":

Hello ---, what application would you like to open?

msword

Creating Child Process To Open Microsoft Word

parent process

Opening 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 Word\n";
        pid_t ProcessID = fork();

        if(ProcessID == -1)
        {
        cout << "Error creating another Process... Exiting\n";
        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 process\n";
        }
    }

int main()
{
    cout << "Hello ---, what application would you like to open?\n";
    string input;
    cin >> input;
    loadApplication(input);


    return 0;
}

1 Answers1

-1

You don't have to use fork/exec for this. Just pass the open command to system():

#include <cstdlib>

int main() {
   system("open /Applications/App\\ Store.app");
   return 0;
}

Note that you will need to escape any spaces in the application name (as shown above), and specify the full name (not just the displayed name).

Here's a very closely related question.

Community
  • 1
  • 1
rhashimoto
  • 15,650
  • 2
  • 52
  • 80