2013-10-31 3 views
0

파일에 결과를 인쇄하는 프로그램이 있다고 가정 해보십시오. 그러나 나는 같은 결과를 다른 파일과 명령 행에도 출력하기를 원합니다. 다른 파일을 만들려고했지만 오류가 계속 발생했습니다 : 해당 파일에 대한 오류 검사를 수행 할 때 "첨자 값도 배열이나 포인터가 아닙니다". 이 일을 어떻게 하죠? 그것은 나를 위해 작동C에서 두 개의 개별 파일과 명령 줄로 출력하는 방법은 무엇입니까?

#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; 
} 
+0

다음은 결과가 하나 개의 파일로 인쇄됩니다 내 프로그램입니다. 콘솔에 출력하고 싶다면'fprintf()'대신에'printf'를 사용하지 않는 것이 어떨까요? – exebook

+0

다른 파일에 쓰려면 방금 하나 이상의 파일 설명자를 선언하고'fopen()'을 한 번 더 호출하십시오. 그래서 그 문제는 무엇입니까? – exebook

+0

printf는 콘솔이 아닌 파일에만 인쇄됩니까? – user2264035

답변

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; 
} 
관련 문제