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]