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]