Suppose I have a program that prints the result to a file. But I want the same result to be printed to another file and the command line as well. I tried creating another file but I kept getting the error: "subscripted value neither an array nor a pointer" when doing the error check for that file. How would I go about doing this? Here is my program where the result is printed to one file:
#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;
}
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int offset;
int ch1, ch2;
FILE *fh1, *fh2, *diffone=stdout, *file2=0;
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);
}
}
if(argc>4) {
if(!(file2 = fopen(argv[4], "w+"))) {
printf("cannot open %s\n",argv[4]); 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);//print to a console or file depending on the value of diffone
if (file2) fprintf(file2,"%c", ch1);
}
}
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.