Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c语言中作为不同文件描述符的多个流_C_File Descriptor - Fatal编程技术网

c语言中作为不同文件描述符的多个流

c语言中作为不同文件描述符的多个流,c,file-descriptor,C,File Descriptor,我有一个程序,我需要提供2个不同的流作为输入 如果我只需要一个流作为输入,我可以像这样使用stdin ./a.out <file1 fgets(buf,1000,stdin) /a.out如果使用bash作为shell,可以执行以下操作: ./a.out 3< file1 4< file2 /a.out 3

我有一个程序,我需要提供2个不同的流作为输入

如果我只需要一个流作为输入,我可以像这样使用stdin

./a.out <file1
fgets(buf,1000,stdin)

/a.out如果使用bash作为shell,可以执行以下操作:

./a.out 3< file1 4< file2
/a.out 3
如果使用bash作为shell,可以执行以下操作:

./a.out 3< file1 4< file2
/a.out 3
您可以这样运行程序:

./a.out file1 file2
int main(int argc, char *argv[]) {

    FILE *fp1 = fopen(argv[1], "r");
    FILE *fp2 = fopen(argv[2], "r");

    fgets(buf1, length_of_buf1, fp1);
    fgets(buf2, length_of_buf2, fp2);
并在程序中使用这些文件,如下所示:

./a.out file1 file2
int main(int argc, char *argv[]) {

    FILE *fp1 = fopen(argv[1], "r");
    FILE *fp2 = fopen(argv[2], "r");

    fgets(buf1, length_of_buf1, fp1);
    fgets(buf2, length_of_buf2, fp2);

您可以这样运行程序:

./a.out file1 file2
int main(int argc, char *argv[]) {

    FILE *fp1 = fopen(argv[1], "r");
    FILE *fp2 = fopen(argv[2], "r");

    fgets(buf1, length_of_buf1, fp1);
    fgets(buf2, length_of_buf2, fp2);
并在程序中使用这些文件,如下所示:

./a.out file1 file2
int main(int argc, char *argv[]) {

    FILE *fp1 = fopen(argv[1], "r");
    FILE *fp2 = fopen(argv[2], "r");

    fgets(buf1, length_of_buf1, fp1);
    fgets(buf2, length_of_buf2, fp2);

您可以为此使用命名管道。或临时文件。您可以为此使用命名管道。或临时文件。