CHAPTER 19
EXTERNAL FILES
Our earlier programs have accessed files
solely through their
standard input and output.
But that's a severe limitation---those programs
can access only one input and one output file.
Fortunately, C provides a well-stocked library
of functions for manipulating external files.
This chapter presents most of these functions
and uses them to write useful file-handling utilities.
We also write a small package that allows us
to use ``virtual arrays,'' data structures that behave like
arrays but are actually stored in files rather than memory.
The chapter concludes with an electronic address book
that stores the addresses and phone numbers
in an indexed external file.
Jump to:
[Previous Chapter |
Next Chapter]
- ACCESSING EXTERNAL FILES
- Character File I/O
- Formatted File I/O
- Line-Oriented File I/O
- THE STANDARD FILES
- RANDOM FILE ACCESS
- BLOCK INPUT AND OUTPUT
- FILE UPDATING
- CASE STUDY: AN ELECTRONIC ADDRESS BOOK
Objectives
- 영구적인 자료 저장 수단으로서의 file 사용 개념을 이해한다.
- File 처리 function들과 이들의 사용법을 익힌다.
- 입출력 function들의 사용법을 익힌다.
File을 사용한 입출력은 file pointer를 사용한다.
FILE *fi, *fo; // 두 개의 file pointer variables fi, fo 선언
File을 사용하기 전에 fopen function을 사용하여 file을 open하여야
한다.
file pointer variable = fopen(file name, mode);
file name : string
mode : "r", "w", 또는 "a"
(read, write, 또는 append)
fi = fopen("input.txt", "r"); // file "input.txt"를
입력모드로 open
fo = fopen("output.txt", "w"); // file "output.txt"를
출력모드로 open
사용이 끝난 file은 fclose function을 사용하여 닫는다.
File I/O functions
| Function | 기능
|
| int getc(FILE *fp) | character 입력
|
| int putc(int c, FILE *fp) | character 출력
|
| char *fgets(char *line, int MaxLine, FILE *fp)
| 한 줄의 string 입력 |
| int fputs(char *line, FILE *fp) | 한 줄의 string 출력
|
| int fscanf(FILE *fp, char *format, arg1, arg2, ...)
| 양식을 갖는 입력 |
| int fprintf(FILE *fp, char *format, arg1, arg2, ...)
| 양식을 갖는 출력 |
| int ungetc(int c, FILE *fp) | 입력한 문자를 다시 file로 원위치
|
세 개의 standard I/O file이 다음 목적으로 "stdio.h"에
정의되어 있다.
stdin : Keyboard 입력
stdout : Monitor 출력
stderr : Error message 출력. 보통 console (monitor의 한 window)
Standard I/O functions
int getchar(void) ==> getc(stdin)과 동일
int putchar(int c) ==> putc(c, stdout)과 동일
char *gets(char *s) ==> fgets(s, stdin)에서 끝의 '\n' character가
제외된 string
int puts(char *s) ==> fputs(s, stdout)에 '\n' 추가
int scanf(char *format, arg1, arg2, ...) ==> int fscanf(stdin,
char *format, arg1, arg2, ...)
int printf(char *format, arg1, arg2, ...) ==> int fprintf(stdout,
char *format, arg1, arg2, ...)
String에서의 입출력
int sscanf(char *string, char *format, arg1, arg2, ...)
int sprintf(char *string, char *format, arg1, arg2, ...)
scanf function 사용
scanf function을 이용해 data를 입력하기 위해서는 입력할 data를
저장할 메모리 주소를 argument로 주어야 한다.
scanf function의 format은 printf function의 format과 유사하며
다음과 같이 처리된다.
- Format string내의 blank space는 임의의 white-space와 match된다.
- % 기호 뒤에 다음과 같이 입력 자료형을 지정한다.
- %d : a decimal integer
- %f, %e, %g : float
- %lf : double
- %c : a character
- %s : a string
- %[...] : [...]에 포함되지 않은 문자가 나올 때 까지 문자열
입력.
- %[^...] : ^ 다음에 지정된 문자가 나올 때 까지 문자열 입력
- %% : % 기호가 입력의 다음 문자가 되어야 한다.
- Format string 내의 다른 모든 문자들은 입력의 문자와 동일하여야
한다.
사용 예) 원소명(element name), 화학기호(chemical symbol), 원자번호(atomic
number), 원자가(atomic weight)가 다음 형식으로 된 자료 입력
(elements.c, elements.dat)
Hydrogen, H, 1, 1.008
Helium, He, 2, 4.003
Lithium, Li, 3, 6.939
nscan = scanf(infile, "%15[^,], %2[^,], %d, %lf%c",
elementName, elementSymbol, &atomicNumber, &atomicWeight,
&termch);
Standard I/O redirection과 pipe (MS-DOS와 UNIX OS에서 유용)
Standard I/O로부터의 입출력을 file 또는 앞 프로세스의 출력으로
전환
Input redirection : prog <infile
Output redirection : prog >outfile
Pipe : prog1 | prog2
실습 1) 한 file을 다른 file로 복사하는 프로그램을 작성하라.
(filecopy.c,
filecpy1.c,
filecpy2.c)
실행 예: "srcfile"을 "dstfile"로 복사
filecopy srcfile dstfile
실습 2) File을 읽어 그 file에 포함된 단어를 한 줄에 하나씩 프린트하고
마지막에 줄, 단어, 문자 수를 프린트하는 프로그램을 작성하라. 각
단어는 white-space로 구분된다. (입력 file은 임의의 C program source
file로 하고 file open version과 input redirection version 두 방법을
시도해 볼 것)
[ Table Of Contents]