c - 从管道命令读取文件名

标签 c pipe filenames

所以我试图让 C 程序以下列格式从命令行读取文件名: 猫(文件名路径) | (程序名称)

当它作为命令行参数输入时,我可以让它读取输入文件的名称,但它不会从连接的参数中读取

这是代码,现在它读取文件名,就像在命令行上的程序名后面写的一样。

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

//initialize file pointer
FILE *file;

//initialize global variables
#define DEFAULT_LEN 70

//main
int main(int argv, char *argc[]){

 //open File for reading
 file = fopen (argc[1],"r");
 //test for failure of file open, if failed, print message, quit
 if(file == NULL){
      printf("I'm sorry Dave, I'm araid I can't do that.\n");
  printf("Open the file \"%s\" that is.\n", argc[1]);
  return(0);
 }

 //read the first line of a file into an array
 char temp[DEFAULT_LEN]; //where the read data is put
 fgets(temp,DEFAULT_LEN,file); //stops at DEFAULT_LEN on \n

 //print out temp
 printf("%s\n", temp);

 //close file, return 0 for main
 fclose(file);
 return(0);
}

任何帮助将不胜感激

最佳答案

你的程序无法获取文件名的原因是你没有给它。

如果您将程序运行为:

prog hello.txt

它在 argc/argv 中给出了参数 hello.txt

但是,您正在做的是:

cat hello.txt | prog

这意味着 shell 正在打开文件并将其输入到程序的标准输入中。实际上,更准确地说,cat 正在打开文件,而 shell 只是将 cat 的标准输出连接到 prog 的标准输入.

解决此问题的一种方法是检查参数的数量(argc 通常是计数,argv[] 是值,尽管您在代码),如果它为零,argc == 1,从标准输入读取您的文件。

只有在给出参数时,您才会打开该文件并阅读它。这就是许多 UNIX 实用程序的工作方式:

od -xcb hello.txt        # will dump the file.
cat hello.txt | od -xcb  # will dump the file, but using standard input.
echo hello | od -xcb     # will dump your "hello" string.

有些甚至根据它们被调用的方式改变它们的行为,wc 就是一个例子 - 如果它知道它们,它会显示文件名:

pax> wc -l qq.c
     29 qq.c
pax> cat qq.c | wc -l
     29
pax> wc -l *.c
      0 a b.c
    168 binmath.c
     49 qq-save.c
     29 qq.c
     11 qq2.c
      5 qqq.c
     18 xx.c
    280 total
pax> cat *.c | wc -l
    280

请注意最后一种情况 - 因为所有文件都通过单个标准输入流显示,所以无法判断有多少文件。 wc 将汇总该流的全部内容。

试试这个:

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

#define DEFAULT_LEN 70

int main (int argc, char *argv[]) {
    FILE *file;

    // Either select standard input or open the given file.

    if (argc == 1) {
        file = stdin;
    } else {
        file = fopen (argv[1], "r");
        if (file == NULL) {
            printf ("I'm sorry Dave, I can't do that\n");
            printf (" (open the file '%s', that is).\n", argv[1]);
            return 1;
        }
    }

    // Now you're connected to stdin or the file itself, no
    //  difference in handling them (unless you do weird fseek
    //  sort of stuff).

    char temp[DEFAULT_LEN];
    fgets (temp, DEFAULT_LEN, file);

    printf ("%s\n", temp);

    // Only close file if you opened it yourself.

    if (argc != 1)
        fclose (file);
    return 0;
}

这允许您使用文件和标准输入法,如下所示:

pax> prog prog.c
#include <stdio.h>

pax> echo hello | prog
hello

关于c - 从管道命令读取文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1563882/

相关文章:

python - 为什么我的 post-receive Hook 在从子进程管道读取时挂起?

python - 调用python脚本并捕获二进制输出

php - SPL 文件信息 : get filename without extension

python - 获取 FTP [Python] 目录中的所有文件名

c# - 通过 C# 对 C 程序的多个输入

c - 有没有8051汇编语言到C代码的转换器

c - Pipeline和Tail命令在C语言中应该如何实现?

windows - 如何手动创建扩展名为 . Windows 中的(点)前缀?例如,.htaccess

c - strcat 错误

c - 如何在 C 中使用来自 #define 的指针和定义