【问题标题】:Leave only a single word in a txttxt中只保留一个单词
【发布时间】:2021-12-04 22:39:33
【问题描述】:

我在 C 中有这段代码,我的 argv[2] 是程序调用中通过参数传递的文件。

    int fd = open("stdout.txt",O_CREAT|O_WRONLY|O_TRUNC,0666);
    dup2(fd, STDOUT);
    close(fd);
    execl("/bin/file","file",argv[2],NULL);

检查文件的扩展名并将输出传递给 txt 文件。她以这种风格返回一个输出:

1212.gif: GIF image data, version 89a, 695 x 343

我需要检查输出(stdout.txt),只留下文件类型所在的部分。我需要检查的文件类型是 PDF、GIF、JPG、PNG、MP4、ZIP 和 HTML。

我怎样才能只留下扩展部分(PDF、GIF、JPG、PNG、MP4、ZIP和HTML)

我可以在 Exec 中使用 linux 命令。

【问题讨论】:

    标签: c file exec


    【解决方案1】:

    您可以使用strchr() 找到第一个. 并复制到一个新字符串

       int posCharoint = strchr('.');
       int posCharDoublePoint = strchr(':');
       char *newString;
       // malloc newString of posCharDoublePoint - posCharPoint
    
       int i = 0;
       while (posCharPoint < posCharDoublePoint) {
          newString[i] = string[posCharPoint];
          i++;
          posCharPoint++;
       }
    
    

    如果要使用 bash 命令,可以使用cut :https://linux.die.net/man/1/cut

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-12
      • 1970-01-01
      • 2021-03-07
      • 1970-01-01
      • 1970-01-01
      • 2022-11-03
      • 2015-03-17
      相关资源
      最近更新 更多