从管道命令读取文件名 [英] Reading a file name from piped command

查看:89
本文介绍了从管道命令读取文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I'm trying to get the C program to read a filename from the command line in the following format: cat (filename path) | (program name)

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

i can get it to read the name of the input file when its entered as a command line argument, but it won't read from the concatenated argument

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

here's the code, right now its reading the name of the file as if written after the program name on the command line.

#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);
}

任何帮助将不胜感激

推荐答案

程序无法获取文件名的原因是因为您没有给它提供文件名.

The reason your program can't get the file name is because you're not giving it to it.

如果您以以下方式运行程序:

If you run your program as:

prog hello.txt

argc/argv中为其指定了参数hello.txt.

但是,您正在做的是:

cat hello.txt | prog

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

which means the shell is opening the file and feeding it into the standard input of your program. Actually, to be more accurate, cat is opening the file and the shell is simply connecting the standard output of cat to the standard input of prog.

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

One way around this is to check the number of arguments (argc is usually the count, argv[] the values, despite the roundabout way you have it in your code) and, if it's zero, argc == 1, read your file from standard input.

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

Only if an argument is given do you open that file and read it. That's the way a lot of UNIX utilities work:

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是一个示例-如果知道文件名,则会显示文件名:

Some even change their behaviour depending on how they're invoked, wc being one example - it shows the file name(s) if it knows them:

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只会计算该流的全部内容.

Note that last case - because all files are being presented over the single standard input stream, there's no way to tell how many files there are. wc will just tally up the entire contents of that stream.

尝试以下方法:

#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;
}

这允许您同时使用文件和标准输入法:

This allows you to use both the file and standard input method as follows:

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

pax> echo hello | prog
hello

这篇关于从管道命令读取文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆