用popen()打开命令行程序? [英] Using popen() to open a program in command line?

查看:357
本文介绍了用popen()打开命令行程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以打开使用其他程序的程序?例如:
我想要在C命令行应用程序,它会提示用户在程序的名称输入(可以说微软Word.app),并且该程序将启动。我会做这样的事情:

 的#include<&stdio.h中GT;
#包括LT&;&time.h中GT;
INT主(INT ARGC,为const char * argv的[]){
    焦炭PROGRAMNAME [1000];
    的printf(节目的名称输入您想开:);
    scanf函数(%S,PROGRAMNAME);
    的popen(PROGRAMNAME);
}

不过,popen()完成要求我和另一个字符。我怎么会去用popen()打开的程序?

编辑:!下面code ++工程

 的#include<&stdio.h中GT;
#包括LT&;&time.h中GT;INT主(INT ARGC,为const char * argv的[]){
    焦炭PROGRAMNAME [1000];
    焦炭应用[100] =的.app;
    焦炭openApp [100] =打开/应用程序/;
    的printf(节目的名称输入您想开:);
    scanf函数(%S,PROGRAMNAME);
    strcat的(openApp,PROGRAMNAME);
    strcat的(openApp,应用);
    系统(openApp);}


解决方案

的popen 让你启动一个程序,并得到一个文件描述符到它的输入或输出,就像的fopen 适用于文件。举例来说,如果你想读你的程序的输出,你会使用的popen(计划,R)。在另一方面,如果你想写到它的输入,你可以使用的popen(计划,W)。的Mac OS X还允许 R + ,它可以让你读取输出和写入输入,但这种能力是不是标准的,跨平台的,不应依赖code。

如果你只是想启动一个程序,你可能也使用系统函数,该函数的和等待,直到程序退出,此时它返回状态code。 系统实际上调用shell来工作,所以争论将发生膨胀(环境变量,〜,等等)。

修改追随你的评论说,系统(微软Word.app)不会如你所期望的工作:有几个原因造成的,其实。你得到的消息开始:这是因为你写的是相当于打开一个终端窗口,键入微软Word.app <​​/ code>。换句话说,它试图找到一个名为微软的程序,然后通过它的说法Word.app。你会需要或者引用程序名称或逃生空间有壳明白这是一个完整的程序名称,而不是一个程序的名字,然后一个参数:系统(微软\\ Word.app)

现在,这应该抱怨说,shell不能找到该程序微软Word.app,这已经是一个进步。

这是因为在Mac OS,应用文件不是可执行文件:它们是文件夹,该搜索器显示为单个文件。您可以验证按ctrl +单击(或右键单击)的应用程序并选择显示包内容(这将打开应用程序文件夹)。微软Word.app实际的可执行文件必须沿着路径某处微软Word.app/Contents/MacOS/Microsoft字

正如你所看到的,这是获得各种复杂的。幸运的是,苹果公司提供了打开可执行文件,可以用一堆OS服务弄清楚这些细节。它允许在推出以下方式申请:

 开放-a微软\\ Word中

这应该推出的话语。 (注意如何仍然需要转义空格)在纯C code,这将让你像这样:

 系统(开放-a微软的Word \\\\);

如果您选择使用的Objective-C和Cocoa,但是,有一个很简单的方法来打开应用程序:

 的NSString *的appName = @的Microsoft Word // 无处可逃!
[NSWorkspace sharedWorkspace] launchApplication:的appName];

的NSString 可以从C字符串创建的对象很容易就够了:

 的NSString *的appName = [[NSString的页头]方法initWithCString:PROGRAMNAME编码:NSUTF8StringEncoding];
[NSWorkspace sharedWorkspace] launchApplication:的appName];
[发布的appName];

Is it possible to open a program using another program? For example: I want to make a command line application in C that will prompt the user to type in the name of a program (lets say Microsoft Word.app), and that program will launch. Would I do something like this:

#include <stdio.h>
#include <time.h>
int main (int argc, const char * argv[]) {
    char programName[1000];
    printf("Type in the name of the program you would like to open: ");
    scanf("%s", programName);
    popen(programName);
}

However, popen() asks me for another char. How would I go about using popen() to open the program?

EDIT: The following code works!

#include <stdio.h>
#include <time.h>

int main (int argc, const char * argv[]) {
    char programName[1000];
    char app[100] = ".app";
    char openApp[100] = "open /Applications/";
    printf("Type in the name of the program you would like to open: ");
    scanf("%s", programName);
    strcat(openApp, programName);
    strcat(openApp, app);
    system(openApp);

}

解决方案

popen lets you launch a program and get a file descriptor to its input or output, much like fopen works for files. For instance, if you wanted to read the output of your program, you'd use popen("program", "r"). On the other hand, if you want to write to its input, you would use popen("program", "w"). Mac OS X also allows for r+, which lets you read the output and write to the input but this capability isn't standard and shouldn't be relied on for cross-platform code.

If you just want to launch a program, you might as well use the system function, which does that and waits until the program exits, at which point it returns the status code. system actually invokes the shell to work, so arguments will undergo expansion (environment variables, ~, etc).

EDIT Following your comment that system("Microsoft Word.app") doesn't work as you'd expect: there are several reasons for this, actually. Starting with the message you get: this is because what you wrote is equivalent to opening a terminal window and typing Microsoft Word.app. In other words, it tries to find a program called "Microsoft", then pass it the argument "Word.app". You would need to either quote the program name or escape spaces to have the shell understand it's a whole program name and not a program name then an argument: system("Microsoft\ Word.app")

Now, this should complain saying that the shell can't find the program "Microsoft Word.app", which is already a step forward.

This is because on Mac OS, app files aren't executable files: they're folders that the Finder displays as a single file. You can verify that by ctrl+clicking (or right-clicking) an app and selecting "Show package contents" (this will open the app folder). The actual executable for Microsoft Word.app must be somewhere along the path of Microsoft Word.app/Contents/MacOS/Microsoft Word.

As you can see, this is getting kind of complex. Luckily enough, Apple provides the open executable, which can use a bunch of OS services to figure out those details. It allows to launch applications in the following fashion:

open -a Microsoft\ Word

This should launch Word. (Notice how you still need to escape the spaces.) In pure C code, that would get you something like this:

system("open -a Microsoft\\ Word");

If you choose to use Objective-C and Cocoa, however, there is a very simple way to open applications:

NSString* appName = @"Microsoft Word"; // no escape!
[[NSWorkspace sharedWorkspace] launchApplication:appName];

NSString objects can be created from C string easily enough:

NSString* appName = [[NSString alloc] initWithCString:programName encoding:NSUTF8StringEncoding];
[[NSWorkspace sharedWorkspace] launchApplication:appName];
[appName release];

这篇关于用popen()打开命令行程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆