Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何在默认程序Linux中打开文件_C_Linux - Fatal编程技术网

C 如何在默认程序Linux中打开文件

C 如何在默认程序Linux中打开文件,c,linux,C,Linux,如何以编程方式在Linux的默认程序中打开文件(我使用的是Ubuntu 10.10) 例如,打开*.mp3将在电影播放器(或其他东西)中打开文件。您需要运行gnome open、kde open或exo open,具体取决于您使用的桌面 我相信有一个名为xdg utils的项目试图为本地桌面提供统一的接口 比如说: snprintf(s, sizeof s, "%s %s", "xdg-open", the_file); system(s); pid = fork(); if (pid ==

如何以编程方式在Linux的默认程序中打开文件(我使用的是Ubuntu 10.10)

例如,打开*.mp3将在电影播放器(或其他东西)中打开文件。

您需要运行gnome open、kde open或exo open,具体取决于您使用的桌面

我相信有一个名为xdg utils的项目试图为本地桌面提供统一的接口

比如说:

snprintf(s, sizeof s, "%s %s", "xdg-open", the_file);
system(s);
pid = fork();
if (pid == 0) {
  execl("/usr/bin/xdg-open", "xdg-open", the_file, (char *)0);
  exit(1);
}
// parent will usually wait for child here
小心代码注入。绕过用户输入脚本层更安全,所以请考虑如下:

snprintf(s, sizeof s, "%s %s", "xdg-open", the_file);
system(s);
pid = fork();
if (pid == 0) {
  execl("/usr/bin/xdg-open", "xdg-open", the_file, (char *)0);
  exit(1);
}
// parent will usually wait for child here

Ubuntu 10.10基于GNOME。所以,使用
g\u应用程序\u信息\u启动\u uri()的默认值\u

这样的办法应该行得通

#包括
#包括
int main(int argc,char*argv[])
{
gboolean-ret;
GError*error=NULL;
g_type_init();
ret=g\u应用程序\u信息\u启动\u默认\u uri(“file:///etc/passwd",
无效的
&误差);
如果(ret)
g_信息(“已工作”);
其他的
g_消息(“nop:%s”,错误->消息);
返回0;
}

顺便说一句,
xdgopen
,一个shell脚本,试图确定您的桌面环境,并调用已知的帮助程序,例如为GNOME调用
gvfs open
,为kde调用
kde open
,等等
gvfs open
最终调用
g\u app\u info\u launch\u default\u以获得\u uri()

一个编码更少的简单解决方案:

我已经在我的Ubuntu上测试了这个程序,它运行得很好,如果我没有错的话,你正在寻找类似的东西


#包括
#包括
int main()
{
系统(“firefoxfile:///dox/song.mp3");
返回0;
}

xdg open
调用合适的一个。@ninjalj,太好了,我希望pkg能做到这一点。我想这是Chrome的用法。虽然我建议使用<代码> ExcV*>代码>代替<代码>系统< /C> > @斯特潘:文件名,如果你使用<代码>系统< /> >,是一个相当明显的攻击向量。无论如何,<代码> Excel(“XDG Onter”、“XDG Onter”、文件(CHAR *)null)更安全。(当然,在
fork()
)之后。如果应用程序已经依赖于GNOME,我更喜欢这种方法。默认情况下,Ubuntu附带firefox,如果您可以从终端运行
firefox
命令。。。。你可以把它做好。。。。看看我的解决方案……看看这是否适合你