2013年12月4日水曜日

開発環境

C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、谷口 功 (翻訳)、オライリー・ジャパン)のⅡ部(単純なプログラミング)の14章(ファイル入出力)、14.1(ファイル関数)、14.2(変換ルーチン)、14.3(バイナリファイルとASCIIファイル)、14.4(行終端にまつわる謎)、14.5(バイナリI/O)、14.6(バッファリングの問題)、14.7(バッファリングを行わないI/O)、14.8(ファイル形式の設計)、14.10(プログラミング実習)、実習 14-4を解いてみる。

その他参考書籍

実習14-4.

コード

sample.c

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

int main(int argc, char *argv[])
{
    char ch;
    int n;
    int flag;
    char pm;
    FILE *in_file;
    FILE *out_file;
    
    if (argc != 3){
        fprintf(stderr, "Error: Wrong number of arguments\n");
        fprintf(stderr, "Usage is: sample <from> <to>\n");
        exit (8);
    }
    in_file = fopen(argv[1], "r");
    if (in_file == NULL){
        fprintf(stderr, "Cannot open %s\n", argv[1]);
        exit (8);
    }
    out_file = fopen(argv[2], "wb");
    if (out_file == NULL){
        fprintf(stderr, "Cannot open %s\n", argv[2]);
        exit (8);
    }
    
    n = 0;
    pm = 1;
    flag = 0;
    while (1){
        ch = fgetc(in_file);
        if (ch == EOF){
            if (flag == 1){
                if (pm == 1){
                    fprintf(out_file, "-%d", n);
                } else {
                    fprintf(out_file, "%d", n);
                }
            }
            break;
        }
        if (ch == '-'){
            pm = -1;
        } else if (isdigit(ch)){
            n = ch - '0' + 10 * n;
            flag = 1;
        } else {
            if (flag == 1){
                if (n % 3 == 0){
                    fprintf(out_file, "%d ", pm * n);
                } else {
                    fprintf(out_file, "%d ", pm * n);
                }
                n = 0;
                flag = 0;
                pm = 1;
            }
        }
    }
    fputc('\n', out_file);
    fclose(in_file);
    fclose(out_file);
    return (0);
}

コード

sample.c

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

int main(int argc, char *argv[])
{
    char ch;
    int n;
    int flag;
    char pm;
    FILE *in_file;
    FILE *out_file;
    
    if (argc != 3){
        fprintf(stderr, "Error: Wrong number of arguments\n");
        fprintf(stderr, "Usage is: sample <from> <to>\n");
        exit (8);
    }
    in_file = fopen(argv[1], "rb");
    if (in_file == NULL){
        fprintf(stderr, "Cannot open %s\n", argv[1]);
        exit (8);
    }
    out_file = fopen(argv[2], "w");
    if (out_file == NULL){
        fprintf(stderr, "Cannot open %s\n", argv[2]);
        exit (8);
    }
    
    n = 0;
    pm = 1;
    flag = 0;
    while (1){
        ch = fgetc(in_file);
        if (ch == EOF){
            if (flag == 1){
                if (pm == 1){
                    fprintf(out_file, "-%d", n);
                } else {
                    fprintf(out_file, "%d", n);
                }
            }
            break;
        }
        if (ch == '-'){
            pm = -1;
        } else if (isdigit(ch)){
            n = ch - '0' + 10 * n;
            flag = 1;
        } else {
            if (flag == 1){
                if (n % 3 == 0){
                    fprintf(out_file, "%d ", pm * n);
                } else {
                    fprintf(out_file, "%d ", pm * n);
                }
                n = 0;
                flag = 0;
                pm = 1;
            }
        }
    }
    fputc('\n', out_file);
    fclose(in_file);
    fclose(out_file);
    return (0);
}

入出力結果(Terminal)

$ cat numbers
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
-1 -2 -3 -4 -5 -6 -7 -8 -9 -10
-11 -12 -13 -14 -15 -16 -17 -18 -19 -20
$ cc -g -o sample sample.c
$ cc -g -o sample1 sample1.c
$ ./sample numbers numbers1
$ ./sample1 numbers1 numbers2
$ cat numbers1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -11 -12 -13 -14 -15 -16 -17 -18 -19 -20 
$ cat numbers2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -11 -12 -13 -14 -15 -16 -17 -18 -19 -20 
$

0 コメント:

コメントを投稿