작은숲:위키노트/C 언어 예제/File 덤프

큰숲백과, 나무를 보지 말고 큰 숲을 보라.
(C 언어 예제/File 덤프에서 넘어옴)
/**
 * Copyright (c) 2001,2002 Yoon, Hyunho <hhyoon@kldp.org>
 * http://mooo.org
 * ----------------------------------------------------------------------
 *
 * LICENSE
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLINE        256
int main(int argc, char *argv[])
{    unsigned int ch, i;
    char fn[MAXLINE];
    FILE *fp;
    /* 만약 명령행 라인을 통해 입력된 값이 있다면 그 파일을 연다 */
    if (argc > 1)
        strcpy(fn, argv[1]);
    else
        strcpy(fn, "data253.txt");
    /* 이진 읽기 모드로 파일을 연다. */
    if ((fp = fopen(fn, "rb")) == NULL) {
        /* 실패한 경우 프로그램을 종료한다. */
        printf("Cannot open the file: %s\n", fn);
        exit(0);
    }
    /* 파일의 마지막을 만날 때까지 반복문을 수행한다. */
    for (i = 0; !feof(fp); i++) {
        /* 줄의 앞머리에 줄 번호를 보여준다. */
        if (i % 8 == 0) printf(" ");
        if (i % 16 == 0) printf("\n%05X:  ", i);
        /* fgetc() 함수를 통해 한 바이트를 읽어들인다. */
        if ((ch = fgetc(fp)) == EOF) break;
        /* 혹은 fread 함수를 이용해서 읽어들일 수도 있다. 
           fread(&ch, sizeof(char), 1, fp); */
        printf("%02X ", ch);
    }
    printf("\n");
    /* 파일을 닫는다. */
    fclose(fp);
    return 0;
}
이 작은숲 문서의 출처는 위키노트의 위키노트/C 언어 예제/File 덤프 문서입니다.