20131005【C言語】ファイルの中身を交換する

お題

ファイルの中身を交換する

 

ソース

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
 FILE *fp1, *fp2, *fptemp;
 char ch;

 /* Check arg count */
 if (argc != 3) {
  fprintf(stderr, "Usage: <Program Name> <File1> <File2>.\n");
  exit(1);
 }

 /* Open File1 for read */
 if *1 == NULL) {
  fprintf(stderr, "Error while opening %s for reading.\n", argv[1]);
  exit(1);
 }

    /* Open File2 for read */
    if *2 == NULL) {
        fprintf(stderr, "Error while opening %s for reading.\n", argv[2]);
        exit(1);
    }

    /* Open temp file */
    if *3 == NULL) {
        fprintf(stderr, "Error while opening temp file.\n");
        exit(1);
    }

 /* Copy from FILE1 to temp */
 while(!feof(fp1)) {
  ch = fgetc(fp1);
  if (ferror(fp1)) {
   fprintf(stderr, "Error while reading %s.\n", argv[1]);
   exit(1);
  }

  if (!feof(fp1))
   fputc(ch, fptemp);
  if (ferror(fptemp)) {
   fprintf(stderr, "Error while writing to temp file from %s.\n", argv[1]);
   exit(1);
  }
 }

 /* Close FILE1 */
 if (fclose(fp1) == EOF) { 
  fprintf(stderr, "Error closing %s readingr writing.\n", argv[1]);
  exit(1);
 }

 /* Close temp file */
 if (fclose(fptemp) == EOF) {
  fprintf(stderr, "Error closing temp file after copy from %s.\n", argv[1]);
  exit(1);
 }

 /* Open FILE1 for write */
    if *4 == NULL) {
        fprintf(stderr, "Error while opening %s for writing.\n", argv[1]);
        exit(1);
    }

    /* Copy from FILE2 to FILE1 */
    while(!feof(fp2)) {
        ch = fgetc(fp2);
        if (ferror(fp2)) {
            fprintf(stderr, "Error while reading %s.\n", argv[2]);
            exit(1);
        }

        if (!feof(fp2))
            fputc(ch, fp1);
        if (ferror(fp1)) {
            fprintf(stderr, "Error while writing to %s from %s.\n", argv[1], argv[2]);
            exit(1);
        }
    }

    /* Close FILE1 */
    if (fclose(fp1) == EOF) {
        fprintf(stderr, "Error closing %s after wriging from %s.\n", argv[1], argv[2]);
        exit(1);
    }

    /* Close FILE2 */
    if (fclose(fp2) == EOF) {
        fprintf(stderr, "Error closing %s after copy from %s.\n", argv[2], argv[1]);
        exit(1);
    }

    /* Open FILE2 for write */
    if *5 == NULL) {
        fprintf(stderr, "Error while opening %s for writing.\n", argv[2]);
        exit(1);
    }

    /* Open temp file for read */
    if *6 == NULL) {
        fprintf(stderr, "Error while opening temp file for reading.\n");
        exit(1);
    }

    /* Copy from temp file to FILE2 */
    while(!feof(fptemp)) {
        ch = fgetc(fptemp);
        if (ferror(fptemp)) {
            fprintf(stderr, "Error while reading temp file.\n");
            exit(1);
        }

        if (!feof(fptemp))
            fputc(ch, fp2);
        if (ferror(fptemp)) {
            fprintf(stderr, "Error while writing to %s from temp file.\n", argv[2]);
            exit(1);
        }
    }

    /* Close FILE2 */
    if (fclose(fp2) == EOF) {
        fprintf(stderr, "Error closing %s after wriging from temp file.\n", argv[2]);
        exit(1);
    }

    /* Close temp file */
    if (fclose(fptemp) == EOF) {
        fprintf(stderr, "Error closing temp file after copy from temp file.\n");
        exit(1);
    }

 exit(0);

}

 

実行結果

# cat file1.txt
This file is file1.
# cat file2.txt
This file is file2.
# ./a.out file1.txt file2.txt
# cat file1.txt
This file is file2.
# cat file2.txt
This file is file1.

*1:fp1 = fopen(argv[1], "rb"

*2:fp2 = fopen(argv[2], "rb"

*3:fptemp = fopen("temp", "wb"

*4:fp1 = fopen(argv[1], "wb"

*5:fp2 = fopen(argv[2], "wb"

*6:fptemp = fopen("temp", "rb"