【问题标题】:Getting filetype from arguments(argv) [closed]从参数(argv)获取文件类型
【发布时间】:2020-07-11 22:19:51
【问题描述】:

所以这是一个应该通过创建新文件夹来将文件分类为文件类型的程序。

假设你跑了

./category -f path/to/file1.jpg path/to/file2.c path/to/file3.zip

然后使用-f./category 程序会将-f 旁边的所有文件分类并放入它们现有的文件类型中。

这与线程、管道、套接字有关,但我不知道是哪一个。无论如何,这是我迄今为止的尝试。有一个.zip,解压后我尝试对内容进行分类。

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>

void unzipfiles();

int main(int argc, char **argv) {

    pthread_t thread1, thread2;

    unzipfiles();


    for (int i=0; i < sizeof(argv); i++) {

        char *mkdr[3] = {"mkdir", argv[i], NULL};

        execv("/usr/bin/mkdir", mkdr);

        char *mv[3] = {"mv","-v", "*.$argv", "argv/", argv[i], NULL};

        execv("/usr/bin/mv", mv);

    }

    printf("This line will not be executed\n");

    return 0;

}

void unzipfiles() {

    char *unzp[3] = {"unzip", "soal3.zip", NULL};
    execv("/usr/bin/unzip", unzp);

}

上面没有我使用的线程、管道或套接字。我还没有掌握使用它的概念

我的问题是,我知道您应该从 argv 获取文件类型,但是如何?

-f 是什么?

非常感谢您的回答

【问题讨论】:

  • 请缩进您的代码。 get the file types from argv, but how? - 从字符串中提取逗号后面的部分。
  • @KamilCuk 你如何从字符串中提取逗号后面的部分?你在说什么逗号?
  • 从您非常简洁的描述中,我能猜到的最好的结果是 -f 是一个命令行标签,指示后面的参数是文件,您的任务是学习使用解析命令行argcargv[]。也许this will help。从命令行获得文件后,您将通过查看文件描述符来确定每个文件的文件类型。 (即它的扩展名)
  • ...我认为@KamilCuk 可能意味着dot(即.)而不是comma(即,
  • how do you extract the part after the comma from the string? - 遍历从最后一个字符到字符串中第一个字符的字符并提取点的位置。然后点之后的部分从带点的位置开始,直到字符串的末尾。 and what comma are you talking about? - 是的,我的意思是一个点,对不起。

标签: c linux bash unix


【解决方案1】:

根据您描述的程序需要执行以下操作:

1) be executed on the command line with arguments: `-f` and some path\file.ext names
2) use `argc` and `argv`to capture command line arguments   
3) test argv[1] for being `-f` 
3) if `argv[1] == -f`, parse each command line starting from `argv[2]` to determine type.

以下是一个非常简单的示例,演示了如何只执行上面列出的最简单的任务:

注意:我使用以下命令行进行了测试:

prog.exe -f path/to/file1.jpg path/to/file2.c path/to/file3.zip

//使用类别 -f file1 file2 file3 ...

int main(int argc, char *argv[])
{
    char *tok = NULL;
    char type[80] = {0};

    if(argc == 1) return 0;

    if(strcmp(argv[1], "-f")==0)
    {
        for(int i = 2;i<argc;i++)//start with first arg 
        {
            tok = strtok(argv[i], ".");
            while(tok)
            {
                strcpy(type, tok);
                tok = strtok(NULL, ".");
            }
            printf("Type %d is: %s\n", i-1, type);
        }
    }

    return 0;
}

结果:

Type 1 is: jpg   
Type 2 is: c     
Type 3 is: zip

【讨论】:

  • 还有一个鲜为人知的strrchrtok = strrchr(argv[i], '.')
  • 非常感谢您的详细解答。你不知道对我来说有多重要。这是五个小时不间断的错误。
  • @KamilCuk - strrchr 是一个非常好的建议。你是对的,鲜为人知。谢谢!
  • @ryyker strtok 会将一系列多个分隔符视为一个分隔符。因此,您将从“asdf.jpg”中获得与从“.asdf...jpg”中获得相同的标记,即使它们的文件名不同。
  • @ryyker idk 如果你还想破解它,但我使用你的代码遇到了分段错误(核心转储)
【解决方案2】:

-f 的使用在 Linux 中是强制的,因此现在您使用的命令 prog.exe -f path/to/file1.jpg path/to/file2.c path/to/file3.zip 必须相应地强制输出每个文件类型

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 2023-01-22
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    • 2015-12-04
    相关资源
    最近更新 更多