简体   繁体   English

Linux重定向到C程序-文件读取

[英]Linux Redirection to C program - File Reading

I want to read lines of numbers from the text file ( filename.txt ) using a function in C. 我想使用C中的函数从文本文件( filename.txt )中读取数字行。

How do I open this file (provided the filename is only given through a redirection on Unix)? 如何打开此文件(假设文件名仅在Unix上通过重定向提供)?

ie ./cfile < filename.txt

int main (void)
{
  char filename[20];
  fgets(filename, 19, stdin);
  FILE *fp;
  fp = fopen(filename, "r");
}

So, would this be correct; 因此,这是正确的吗? also, how do I access one line at a time from the file (all I know is EOF has to be used somewhere)? 另外,如何一次从文件访问一行(我所知道的是必须在某处使用EOF)?

The contents of filename.txt gets redirected to the standard input of the executable. filename.txt内容将重定向到可执行文件的标准输入。 Therefore you could simply write your code as follows: 因此,您可以简单地编写如下代码:

#include <stdio.h>

#define MAXLEN 256 // Maximum number of characters in a line

int main() {
   char line[MAXLEN];
   while (fgets(line, MAXLEN, stdin)) {
       printf("Line %s", line); // Do something with the line
   }
   return 0;
}

< is used to redirect the standard input from a file instead of the keyboard, in this case you don't need fopen : <用于从文件而不是键盘重定向标准输入,在这种情况下,您不需要fopen

int main(void)
{
    char buffer[100];

    /* Don't use magic numbers with fgets, sizeof buffer is the correct size */
    while (fgets(buffer, sizeof buffer, stdin)) {
        printf("%s", buffer);
    } 
    return 0;
}

You can fopen a file using an argument passed to main: 您可以使用传递给main的参数来fopen文件:

int main(int argc, char *argv[])
{
    char buffer[100];
    FILE *fp;

    if (argc == 2) {
        fp = fopen(argv[1], "r");
        if (fp == NULL) {
            perror("fopen");
            exit(EXIT_FAILURE);
        }
        while (fgets(buffer, sizeof buffer, fp)) {
            printf("%s", buffer);
        } 
        fclose(fp);
    }
    return 0;
}

Launch it using ./cfile filename.txt (without the redirection). 使用./cfile filename.txt (不带重定向)启动它。

On Linux, when you run a program, 3 files are opened for the standard input (fd 0), output (fd 1) and error (fd 2). 在Linux上,运行程序时,将为标准输入(fd 0),输出(fd 1)和错误(fd 2)打开3个文件。 By default, these files are your terminal : 默认情况下,这些文件是您的终端:

 % cat & 
 % ls -l /proc/`pidof cat`/fd
total 0
lrwx------ 1 max max 64 mars  21 10:34 0 -> /dev/pts/0
lrwx------ 1 max max 64 mars  21 10:34 1 -> /dev/pts/0
lrwx------ 1 max max 64 mars  21 10:34 2 -> /dev/pts/0

But you can also specify the file that should be used with <, > and 2> : 但是,您也可以指定应与<,>和2>一起使用的文件:

 % cat > /dev/null &        
 % ls -l /proc/`pidof cat`/fd
total 0
lrwx------ 1 max max 64 mars  21 10:36 0 -> /dev/pts/0
l-wx------ 1 max max 64 mars  21 10:36 1 -> /dev/null
lrwx------ 1 max max 64 mars  21 10:36 2 -> /dev/pts/0

So, the file is already opened and is respectively in the FILE* stdin , stdout and stderr . 因此,该文件已经打开,并且分别位于FILE* stdinstdoutstderr For this last example it's in stdout but in your case (with <) it will be in the FILE* stdin . 对于最后一个示例,它位于stdout中,但在您的情况下(带有<),它将位于FILE* stdin

So basically, you don't need to do the fopen , and you can use stdin . 因此,基本上,您不需要执行fopen ,您可以使用stdin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM