在C++程序中打开微软Word
正如标题所提到的,我正试图通过这个程序打开MicrosoftWord,但遇到了一点困难。在对进程进行了一些研究之后,我决定使用进程ID和Fork函数来处理在程序中打开另一个文件的问题。我似乎遇到一些困难的领域是exec系列函数。对于这些函数,它们似乎有各种不同的用途,但我很难思考应该使用哪个函数,以及我是否在语法上正确地列出了我的论点 键入msword时,我的控制台会将以下内容打印到屏幕上: 您好--,您想打开什么应用程序 msword 创建子进程以打开Microsoft Word在C++程序中打开微软Word,c++,macos,C++,Macos,正如标题所提到的,我正试图通过这个程序打开MicrosoftWord,但遇到了一点困难。在对进程进行了一些研究之后,我决定使用进程ID和Fork函数来处理在程序中打开另一个文件的问题。我似乎遇到一些困难的领域是exec系列函数。对于这些函数,它们似乎有各种不同的用途,但我很难思考应该使用哪个函数,以及我是否在语法上正确地列出了我的论点 键入msword时,我的控制台会将以下内容打印到屏幕上: 您好--,您想打开什么应用程序 msword 创建子进程以打开Microsoft Word #inclu
#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;
}
父进程
打开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;
}
您不必为此使用fork/exec。只需将命令传递给: 请注意,您需要如上所示转义应用程序名称中的所有空格,并指定全名,而不仅仅是显示的名称 这是一个非常好的例子