0

I'm trying to write a program that allows me to type something like this on the command line:

cat inputfile | myprogram outputfile

In myprogram I need to open both the input and output file without the input file being a parameter. I know how to open the output file and set it to a descriptor:

int out=open(argv[1],O_RDONLY);

But I don't know how to do this with the input file. I know the output of cat is sent to the cin of my program but I don't want to read the file in line by line. Is it possible to do this in a way similar to opening the output file? Like so:

int in=open(?????,O_RDONLY);
4

1 回答 1

1

在 C 中,它可以像这样解决

#include<stdio.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc, char **argv)
{
        int flags=fcntl(0,F_GETFL);
        fcntl(0,F_SETFL,flags|O_NONBLOCK);
        char ch;
        while(read(0,&ch,1))
        {
                printf("%c",ch);
        }
}
于 2013-04-10T05:44:51.473 回答