如何在c程序中重定向多个文本文件

如何在c程序中重定向多个文本文件,c,redirect,C,Redirect,如何在c程序中重定向多个文本文件?例如,我有以下C代码: //redirection.c #include<stdio.h> main() { int x,y; scanf("%d",&x); x=x*x; printf("%d",x); scanf("%d",&y); y=x+y; printf("%d",y); } //redirection.c #包括 main() { int x,y; scanf(“%d”和&x); x=x*x; printf(“%d”,

如何在c程序中重定向多个文本文件?例如,我有以下C代码:

//redirection.c
#include<stdio.h>
main()
{
int x,y;
scanf("%d",&x);
x=x*x;
printf("%d",x);

scanf("%d",&y);
y=x+y;
printf("%d",y);
}
//redirection.c
#包括
main()
{
int x,y;
scanf(“%d”和&x);
x=x*x;
printf(“%d”,x);
scanf(“%d”和“&y”);
y=x+y;
printf(“%d”,y);
}
编译完这段代码后,我创建了两个文本文件text1.txt的值为8,text2.txt的值为6


当我使用命令行重定向(as
redirection向该程序提供输入时,您还可以使用命令行参数:

#include <stdio.h>

#define BUFSIZE 1000

int main(int argc, char *argv[])
{
    FILE *fp1 = NULL, *fp2 = NULL;
    char buff1[BUFSIZE], buff2[BUFSIZE];

    fp1 = fopen(argv[1], "r");
    while (fgets(buff1, BUFSIZE - 1, fp1) != NULL)
    {
        printf("%s\n", buff1);
    }
    fclose(fp1);

    fp2 = fopen(argv[2], "r");
    while (fgets(buff2, BUFSIZE - 1, fp2) != NULL)
    {
        printf("%s\n", buff2);
    }
    fclose(fp2);
}
#包括
#定义BUFSIZE 1000
int main(int argc,char*argv[])
{
文件*fp1=NULL,*fp2=NULL;
字符buff1[BUFSIZE],buff2[BUFSIZE];
fp1=fopen(argv[1],“r”);
while(fgets(buff1,BUFSIZE-1,fp1)!=NULL)
{
printf(“%s\n”,buff1);
}
fclose(fp1);
fp2=fopen(argv[2],“r”);
while(fgets(buff2,BUFSIZE-1,fp2)!=NULL)
{
printf(“%s\n”,buff2);
}
fclose(fp2);
}
下面是一个更干净的版本:

#include <stdio.h>

#define BUFSIZE 1000
void print_content(char *file);
int main(int argc, char *argv[])
{
    print_content(argv[1]);
    print_content(argv[2]);
}

void print_content(char *file){
    char buff[BUFSIZE];
    FILE *fp = fopen(file, "r");

    while (fgets(buff, sizeof(buff), fp) != NULL)
    {
        printf("%s\n", buff);
    }
    fclose(fp);
}
#包括
#定义BUFSIZE 1000
作废打印内容(字符*文件);
int main(int argc,char*argv[])
{
打印内容(argv[1]);
打印内容(argv[2]);
}
无效打印内容(字符*文件){
字符buff[BUFSIZE];
文件*fp=fopen(文件“r”);
while(fgets(buff,sizeof(buff),fp)!=NULL)
{
printf(“%s\n”,浅黄色);
}
fclose(fp);
}

您还可以使用命令行参数:

#include <stdio.h>

#define BUFSIZE 1000

int main(int argc, char *argv[])
{
    FILE *fp1 = NULL, *fp2 = NULL;
    char buff1[BUFSIZE], buff2[BUFSIZE];

    fp1 = fopen(argv[1], "r");
    while (fgets(buff1, BUFSIZE - 1, fp1) != NULL)
    {
        printf("%s\n", buff1);
    }
    fclose(fp1);

    fp2 = fopen(argv[2], "r");
    while (fgets(buff2, BUFSIZE - 1, fp2) != NULL)
    {
        printf("%s\n", buff2);
    }
    fclose(fp2);
}
#包括
#定义BUFSIZE 1000
int main(int argc,char*argv[])
{
文件*fp1=NULL,*fp2=NULL;
字符buff1[BUFSIZE],buff2[BUFSIZE];
fp1=fopen(argv[1],“r”);
while(fgets(buff1,BUFSIZE-1,fp1)!=NULL)
{
printf(“%s\n”,buff1);
}
fclose(fp1);
fp2=fopen(argv[2],“r”);
while(fgets(buff2,BUFSIZE-1,fp2)!=NULL)
{
printf(“%s\n”,buff2);
}
fclose(fp2);
}
下面是一个更干净的版本:

#include <stdio.h>

#define BUFSIZE 1000
void print_content(char *file);
int main(int argc, char *argv[])
{
    print_content(argv[1]);
    print_content(argv[2]);
}

void print_content(char *file){
    char buff[BUFSIZE];
    FILE *fp = fopen(file, "r");

    while (fgets(buff, sizeof(buff), fp) != NULL)
    {
        printf("%s\n", buff);
    }
    fclose(fp);
}
#包括
#定义BUFSIZE 1000
作废打印内容(字符*文件);
int main(int argc,char*argv[])
{
打印内容(argv[1]);
打印内容(argv[2]);
}
无效打印内容(字符*文件){
字符buff[BUFSIZE];
文件*fp=fopen(文件“r”);
while(fgets(buff,sizeof(buff),fp)!=NULL)
{
printf(“%s\n”,浅黄色);
}
fclose(fp);
}

同时将输入作为重定向,如下所示

cat a b | ./a.out.
或者,您可以使用命令行参数

#include<stdio.h>
main(int argc, char *argv[])
{
    FILE *fp, *fp1;
    if ( (fp=fopen(argv[1],"r")) == NULL ){
            printf("file cannot be opened\n");
            return 1;
    }
    if (( fp1=fopen(argv[2],"r")) == NULL ){
     printf("file cannot be opened\n");
            return 1;
    }
    int x,y;
    fscanf(fp,"%d",&x);// If you having  only the value in that file
    x=x*x;
    printf("%d\n",x);
    fscanf(fp1,"%d",&y);// If you having  only the value in that file                                       
    y=x+y;
    printf("%d\n",y);

}
#包括
main(int argc,char*argv[])
{
文件*fp,*fp1;
if((fp=fopen(argv[1],“r”)==NULL){
printf(“无法打开文件\n”);
返回1;
}
if((fp1=fopen(argv[2],“r”)==NULL){
printf(“无法打开文件\n”);
返回1;
}
int x,y;
fscanf(fp,'%d',&x);//如果该文件中只有该值
x=x*x;
printf(“%d\n”,x);
fscanf(fp1,“%d,&y);//如果该文件中只有该值
y=x+y;
printf(“%d\n”,y);
}

同时将输入作为重定向,如下所示

cat a b | ./a.out.
或者,您可以使用命令行参数

#include<stdio.h>
main(int argc, char *argv[])
{
    FILE *fp, *fp1;
    if ( (fp=fopen(argv[1],"r")) == NULL ){
            printf("file cannot be opened\n");
            return 1;
    }
    if (( fp1=fopen(argv[2],"r")) == NULL ){
     printf("file cannot be opened\n");
            return 1;
    }
    int x,y;
    fscanf(fp,"%d",&x);// If you having  only the value in that file
    x=x*x;
    printf("%d\n",x);
    fscanf(fp1,"%d",&y);// If you having  only the value in that file                                       
    y=x+y;
    printf("%d\n",y);

}
#包括
main(int argc,char*argv[])
{
文件*fp,*fp1;
if((fp=fopen(argv[1],“r”)==NULL){
printf(“无法打开文件\n”);
返回1;
}
if((fp1=fopen(argv[2],“r”)==NULL){
printf(“无法打开文件\n”);
返回1;
}
int x,y;
fscanf(fp,'%d',&x);//如果该文件中只有该值
x=x*x;
printf(“%d\n”,x);
fscanf(fp1,“%d,&y);//如果该文件中只有该值
y=x+y;
printf(“%d\n”,y);
}


use pipe
cat text1.txt text2.txt | redirection
谢谢…cat命令工作了。MS DOS中也有类似的命令吗?
type text1.txt text2.txt 2>nul | redirection
你能解释一下type命令中(2)和(nul)的用法吗?我们不能把上面的命令写成“键入text1.txt text2.txt |重定向"?@BLUEPIXY
键入
命令以显示要处理的文件名。
2>nul
以禁止放弃其输出。因此,将命令设置为
键入text1.txt text2.txt |重定向
显示为混乱。使用管道
cat text1.txt text2.txt |重定向
谢谢…cat命令工作。是否有类似的命令d在MS DOS中也是?
键入text1.txt text2.txt 2>nul |重定向
您能解释一下type命令中(2)和(nul)的用法吗?我们不能将上面的命令写成“type text1.txt text2.txt |重定向”吗?@BLUEPIXY
type
命令以显示要处理的文件名。
2>nul
以禁止放弃其输出。因此,将命令作为
type text1.txt text2.txt | redirection
显示为一个混乱。您可以更好地将打开/读取/关闭代码打包到一个函数中,并将该函数的调用打包到一个循环中,你不会吗?对N个参数进行泛化,而不是要求(并检查)正好2个参数。这是真的,谢谢你的建议。因为他对这个解决方案应该做的文件数量很明确。在错误检查方面有点吝啬(
argc
应在使用
argv
元素之前进行检查;
fp
的值应在读取之前进行检查),但它看起来更好。如果您不喜欢我所做的编辑,您可以还原,但将数组和索引传递给函数而不仅仅是数组的元素并不是明显的选择。虽然这是一种可行的方法,但问题显然是关于“重定向”,此方法不使用。您可以更好地将打开/读取/关闭代码打包到函数中,并将该函数的调用打包到循环中,不是吗?使用N个参数的泛化,而不是要求(和检查)正是2个参数。这是真的,谢谢你的建议。因为他对这个解决方案应该执行的文件数很明确。