CHAPTER 19
EXTERNAL FILES

[IMAGE: An Quarter-Size Version of The Joy of C Front Cover]
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]


  1. ACCESSING EXTERNAL FILES
  2. THE STANDARD FILES
  3. RANDOM FILE ACCESS
  4. BLOCK INPUT AND OUTPUT
  5. FILE UPDATING
  6. CASE STUDY: AN ELECTRONIC ADDRESS BOOK

Objectives


File�� ����� ������� file pointer�� ����Ѵ�.



File�� ����ϱ� ���� fopen function�� ����Ͽ� file�� open�Ͽ��� �Ѵ�.

file pointer variable = fopen(file name, mode);
file name : string
mode : "r", "w", �Ǵ� "a" (read, write, �Ǵ� append)



����� ���� 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"�� ���ǵǾ� �ִ�.



Standard I/O functions



String������ �����



scanf function ���

scanf function�� �̿��� data�� �Է��ϱ� ���ؼ��� �Է��� data�� ������ �޸� �ּҸ� argument�� �־�� �Ѵ�.
scanf function�� format�� printf function�� format�� �����ϸ� ������ ���� ó���ȴ�.



��� ��) ���Ҹ�(element name), ȭ�б�ȣ(chemical symbol), ���ڹ�ȣ(atomic number), ���ڰ�(atomic weight)�� ���� �������� �� �ڷ� �Է� (elements.c, elements.dat)



Standard I/O redirection�� pipe (MS-DOS�� UNIX OS���� ����)

Standard I/O�κ����� ������� file �Ǵ� �� ���μ����� ������� ��ȯ




�ǽ� 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]