C 从管道命令读取文件名

C 从管道命令读取文件名,c,pipe,filenames,C,Pipe,Filenames,因此,我试图让C程序以以下格式从命令行读取文件名: cat(文件名路径)|(程序名) 我可以让它在作为命令行参数输入时读取输入文件的名称,但它不会从串联参数中读取 下面是代码,现在它正在读取文件名,就像在命令行上写在程序名之后一样 #include <stdio.h> #include <string.h> //initialize file pointer FILE *file; //initialize global variables #define DEFAU

因此,我试图让C程序以以下格式从命令行读取文件名: cat(文件名路径)|(程序名)

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

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

#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);
}
#包括
#包括
//初始化文件指针
文件*文件;
//初始化全局变量
#定义默认值\u LEN 70
//主要
int main(int argv,char*argc[]){
//打开文件进行读取
file=fopen(argc[1],“r”);
//测试文件打开失败,如果失败,打印消息,退出
if(file==NULL){
printf(“对不起,戴夫,对不起,我不能那样做。\n”);
printf(“打开文件\%s\”,即,argc[1]);
返回(0);
}
//将文件的第一行读入数组
char temp[DEFAULT_LEN];//读取数据的放置位置
fgets(temp,DEFAULT_LEN,file);//在默认_LEN打开时停止\n
//打印输出温度
printf(“%s\n”,temp);
//关闭文件,返回0作为主
fclose(文件);
返回(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;
}
#包括
#包括
#定义默认值\u LEN 70
int main(int argc,char*argv[]){
文件*文件;
//选择标准输入或打开给定文件。
如果(argc==1){
file=stdin;
}否则{
file=fopen(argv[1],“r”);
if(file==NULL){
printf(“对不起,戴夫,我不能那样做”\n”);
printf(“(打开文件'%s',即)。\n”,argv[1]);
返回1;
}
}
//现在您已连接到stdin或文件本身,不是吗
//处理它们时的差异(除非你做了奇怪的fseek
//类似的东西)。
字符温度[默认值];
fgets(临时、默认、文件);
printf(“%s\n”,temp);
//仅当您自己打开文件时才关闭它。
如果(argc!=1)
fclose(文件);
返回0;
}
这允许您使用文件和标准输入法,如下所示:

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

pax> echo hello | prog
hello
pax>prog.c
#包括
帕克斯>回声你好|节目
你好

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

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

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;
}
#包括
#包括
#定义默认值\u LEN 70
int main(int argc,char*argv[]){
文件*文件;
//选择标准输入或打开给定文件。
如果(argc==1){
file=stdin;
}否则{
file=fopen(argv[1],“r”);
if(file==NULL){
printf(“对不起,戴夫,我不能那样做”\n”);
printf(“(打开文件'%s',即)。\n”,argv[1]);
返回1;
}
}
//现在您已连接到stdin或文件本身,不是吗
//处理它们时的差异(除非你做了奇怪的fseek
//类似的东西)。
字符温度[默认值];
fgets(临时、默认、文件);
printf(“%s\n”,temp);
//仅当您自己打开文件时才关闭它。
如果(argc!=1)
fclose(文件);
返回0;
}
这允许您使用文件和标准输入法,如下所示:

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

pax> echo hello | prog
hello
pax>prog.c
#包括
帕克斯>回声你好|节目
你好

将内容输送到流程中不会将值放入
argv
;相反,它将值放入该进程的
stdin

您需要检查
argc
是否大于1。如果是,那么