【问题标题】:Open / lunch mutiple pdf file at ones with C?用 C 打开/午餐多个 pdf 文件?
【发布时间】:2014-03-20 02:46:41
【问题描述】:

我正在尝试精简 C。

我已经制作了这个 lille 程序/守护进程。

意思是你的'Taskfolder'中的任何.pdf-->(ROOTFOLDER)。

将通过“evince”在启动时开放/午餐。

我需要引导中止线程还是有更好的方法来做到这一点?

我尝试过类似 system("envice 1.pdf && envice 2.pdf ...");

但这仍然只是一次打开一个pdf文件。

代码如下所示。

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>

#define ROOTFOLDER ((const unsigned char *)"/home/myusername/Taskfolder/")
#define LUNCHERAPP ((const unsigned char *)"evince ")

int main()
{
    DIR *d;
    struct dirent *dir;
    char *ext;
    d = opendir(ROOTFOLDER);
    if(d)
    {
        int i=0;
        char *tmp;
        while((dir = readdir(d)) != NULL)
        {
            ext = strchr(dir->d_name, '.');
            if((ext != NULL) && (ext != 0) && (strcmp(strchr(ext ,'.'), ".pdf") == 0))
            {
                tmp = malloc(strlen(LUNCHERAPP) + strlen(ROOTFOLDER) + strlen(dir->d_name) + 1);
                strcat(tmp, LUNCHERAPP);
                strcat(tmp, ROOTFOLDER);
                strcat(tmp, dir->d_name);
                printf("pdf== %s\n", tmp);
                system(tmp);// <-- 'evince ~/Taskfolder/filename.pdf'
                free(tmp);
                ++i;
            }
        }
        closedir(d);
    }
    return 0;
}

更新代码:

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>

#define APP ((const unsigned char *)"evince")
#define ROOTFOLDER ((const unsigned char *)"./")
int main()
{
    DIR *d;
    struct dirent *dir;
    char *ext;
    d = opendir(ROOTFOLDER);
    if(d)
    {
        char *cmd = malloc(1);
        strcat(cmd, APP);
        while((dir = readdir(d)) != NULL)
        {
            ext = strchr(dir->d_name, '.');
            if((ext != NULL) && (ext != 0) && (strcmp(ext, ".pdf") == 0))
            {
                cmd = realloc(cmd, strlen(cmd)+strlen(ROOTFOLDER)+strlen(dir->d_name)+1);
                strcat(cmd, " ");
                strcat(cmd, ROOTFOLDER);
                strcat(cmd, dir->d_name);
            }
        }
        closedir(d);
        printf("\nrun:\n%s\n", cmd);
        system(cmd);
        free(cmd);
    }
    return 0;
}

【问题讨论】:

  • strchr(ext ,'.') 可能返回 0。
  • 谢谢 :) 所以我需要添加一个 if(ext != 0)..

标签: c gcc terminal


【解决方案1】:

我不熟悉envice,但是这个命令:

"envice 1.pdf && envice 2.pdf ..." 

将为每个文件执行一次enice。我不认为那是你的本意。我认为您打算使用其命令行上的所有文件执行一次enice。如果正确,您需要省略 &amp;&amp; 并将其格式化如下:

"envice 1.pdf 2.pdf ..." 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-21
    • 1970-01-01
    • 2010-11-21
    • 2010-10-16
    • 2016-12-14
    • 2012-02-07
    • 1970-01-01
    • 2016-01-17
    相关资源
    最近更新 更多