假设我有一个将结果打印到文件的程序。但我希望将相同的结果打印到另一个文件和命令行。我尝试创建另一个文件,但在对该文件进行错误检查时,我不断收到错误消息:“下标值既不是数组也不是指针”。我该怎么做呢?这是我的程序,其中结果打印到一个文件:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int offset;
int ch1, ch2;
FILE *fh1, *fh2, *diffone=stdout;
if( argc<3 ) {
printf("need two file names\n"); return(1);
}
if(!(fh1 = fopen(argv[1], "r"))) {
printf("cannot open %s\n",argv[1]); return(2);
}
if(!(fh2 = fopen(argv[2], "r"))) {
printf("cannot open %s\n",argv[2]); return(3);
}
if(argc>3) {
if(!(diffone = fopen(argv[3], "w+"))) {
printf("cannot open %s\n",argv[3]); return(4);
}
}
while((!feof(fh1)) && (!feof(fh2)))
{
ch1=ch2='-';
if(!feof(fh1)) ch1 = getc(fh1);
if(!feof(fh2)) ch2 = getc(fh2);
if(ch1 != ch2)
fprintf(diffone,"%c", ch1);//How do I print this to another file and the command line as well?
}
return 0;
}