14. fiile io2. 2
File 豢
則 讌蠍蟾讌 襦蠏碁 豢
: Keyboard
豢: Monitor
則 襭襯 殊 曙伎 豢レ 朱
蟾?
蟲
1 80 90 70
2 80 60 40
3 60 50 70
C program
蟲 蠏
1 80 90 70 80
2 80 60 40 60
3 60 50 70 60
3. 3
File 豢
則 殊 伎 豢 谿
企 願鍵
殊 所鍵
殊 郁鍵
蠍
fopen
fscanf, fprintf, fgets, fputs,
fclose
4. 4
File Operation Function fopen()
則 fopen()
碁 file 襦蠏碁 蠍 譴觜る 覈
レ 伎 覈 殊 fopen 牛 譴觜伎
Return螳: FILE 蟲譟一牡 pointer
殊 所鍵 轟 郁鍵 fopen 覦 FILE pointer襯
牛伎 企伎
則 fopen() Syntax
FILE *fopen( const char *filename, const char *mode );
file pointer襦 return disk filename file open mode
5. 5
File Operation Function fopen()
則 fopen()
殊 襭襯 所鍵 伎 殊 譴觜
my_file.txt 所鍵覈襦 譴觜
襷 殊 朱 NULL 覦
FILE *fp ;
fp = fopen(my_file.txt, r);
6. 6
File Operation Function fopen()
則 fopen()
殊 襭襯 郁鍵 伎 殊 譴觜
my_file.txt襯 郁鍵覈襦 譴觜
襷 殊 朱 my_file.txt 焔
襷 企 my_file.txt螳 譟伎覃, 蠏 覈 伎
讌讌
FILE *fp ;
fp = fopen(my_file.txt, w);
7. 7
File Operation Function fopen()
則 fopen()
殊 襭襯 豢螳襦 郁鍵 伎 殊 譴觜
my_file.txt襯 append覈襦 譴觜
襷 殊 朱 my_file.txt 焔
襷 企 my_file.txt螳 譟伎覃, 蠍一ヾ 伎 蠏碁襦
讌螻 襦 一讌 蟆 襷讌襷 豢螳
FILE *fp ;
fp = fopen(my_file.txt, a);
8. 8
File Operation Function fopen()
則 fopen()
殊 襭襯 所 郁鍵襯 伎 殊 譴觜
my_file.txt 所鍵 郁鍵螳 覈 螳ロ 覈襦 譴觜
襷 my_file.txt 朱 NULL 覦
襷 my_file.txt 朱 open螻, my_file.txt
伎 覈 讌
所鍵 郁鍵 殊 襷 豌 豺
FILE *fp ;
fp = fopen(my_file.txt, r+);
9. 9
File Operation Function fopen()
則 fopen()
殊 襭襯 所 郁鍵襯 伎 殊 譴觜
my_file.txt 所鍵 郁鍵螳 覈 螳ロ 覈襦 譴觜
襷 my_file.txt 朱 my_file.txt螳 焔
襷 my_file.txt 朱 open覃伎, my_file.txt
伎 覈 讌讌
FILE *fp ;
fp = fopen(my_file.txt, w+);
10. 10
File Operation Function fopen()
則 fopen()
殊 襭襯 所 郁鍵襯 伎 殊 譴觜
my_file.txt 所鍵 郁鍵螳 覈 螳ロ 覈襦 譴觜
襷 my_file.txt 朱 my_file.txt螳 焔
襷 my_file.txt 朱 open螻, my_file.txt
伎 覈 讌
所鍵 郁鍵 殊 襷 襷讌襷
FILE *fp ;
fp = fopen(my_file.txt, a+);
11. 11
File pointer
則 File pointer
File I/O disk file覈 file pointer襯 牛 stream
燕 .
則 FILE Type
stdio.h file typedef襦 type朱 file
覲企ゼ 襦 struct type
[Ex]
FILE *fp; /* file pointer襦 fp
*/
12. 12
File Operation Function fclose()
則 fclose()
fopen朱 open 殊 覃 fclose襯 覦
殊 .
則 fclose() Syntax
int fclose( FILE *stream );
open file pointer name
Returns 0 : successful
EOF : error
(EOF is defined as 1 in stdio.h)
13. 13
File Operation Function fopen()
則 fclose()
#include <stdio.h>
int main() {
FILE *fp ;
fp = fopen(my_file.txt, r);
...
fclose( fp ) ;
return 0
}
14. 14
Formatted I/O Function fprintf()
則 fprintf()
豌襴襯 printf()覓語企.
豌 語 fopen 伎 郁鍵 螳ロ 覈襦 open
誤一企
蠏 碁 printf 狩
fprintf 伎 豢ル 蟆郁骸 abc.txt 蠍磯.
FILE* fp = fopen( abc.txt, w ) ;
fprintf( fp, %d %dn, 1, 2 );
15. 15
Formatted I/O Function fprintf()
則 fscanf()
豌襴襯 scanf()覓語企.
豌 語 fopen 伎 所鍵 螳ロ 覈襦 open
誤一企
蠏 碁 scanf 狩
abc.txt 伎 scanf 伎 襦蠏碁 企 曙企れ讌
int i, j ;
FILE* fp = fopen( abc.txt, r ) ;
fscanf( fp, %d %d, &i, &j );
16. 16
feof() Function
則 feof()
file pointer螳 file 蟾讌 曙讌襯 覲企
int feof( FILE *stream );
open file pointer name
Returns none-zero : EOF 蟆曙
0 : EOF螳 蟆曙
17. 17
Formatted I/O Function fprintf() and fscanf()
#include <stdio.h>
int main() {
int i, j, k ;
FILE *ifp, *ofp ;
ifp = fopen("abc.txt", "r" ) ;
ofp = fopen("cal.txt", "w" ) ;
fscanf( ifp, "%d %d %d", &i, &j, &k ) ;
fprintf( ofp, "sum: %dn", i + j + k ) ;
fclose( ifp ) ;
fclose( ofp ) ;
return 0 ;
}
則 fprintf(), fscanf()
1 2 3
abc.txt
cal.txt
18. 18
Formatted I/O Function fprintf() and
fscanf()
#include <stdio.h>
int main() {
int i, j, k ;
FILE *ifp, *ofp ;
ifp = fopen("abc.txt", "r" ) ;
ofp = fopen("cal.txt", "w" ) ;
fscanf( ifp, "%d %d %d", &i, &j, &k ) ;
fprintf( ofp, "sum: %dn", i + j + k ) ;
fclose( ifp ) ;
fclose( ofp ) ;
return 0 ;
}
則 fprintf(), fscanf()
1 2 3
abc.txt
This is a
file.
cal.txt
19. 19
Formatted I/O Function fprintf() and
fscanf()
#include <stdio.h>
int main() {
int i, j, k ;
FILE *ifp, *ofp ;
ifp = fopen("abc.txt", "r" ) ;
ofp = fopen("cal.txt", "a" ) ;
fscanf( ifp, "%d %d %d", &i, &j, &k ) ;
fprintf( ofp, "sum: %dn", i + j + k ) ;
fclose( ifp ) ;
fclose( ofp ) ;
return 0;
}
則 fprintf(), fscanf()
1 2 3
abc.txt
This is a
file.
cal.txt
20. 20
Formatted I/O Function fprintf() and
fscanf()
#include <stdio.h>
int main() {
char c ;
FILE *ifp, *ofp ;
ifp = fopen("abc.txt", "r" ) ;
ofp = fopen("abc2.txt", "a" ) ;
while( feof(ifp) == 0 ) {
fscanf( ifp, "%c", &c ) ;
fprintf( ofp, "%c", c ) ;
}
fclose( ifp ) ;
fclose( ofp ) ;
return 0;
}
則 fprintf(), fscanf()
This is a
file.
1 2 3
abc.txt
21. 21
蠍壱 豢
則 fgetc() , getc()
file襦 覿 覓語襯 暑 .
襷 伎 曙 覓語螳 朱 EOF襯 覦
fgetc getc 狩.
char c ;
FILE *fp = fopen("abc.txt", "r" ) ;
while( (c = fgetc(fp)) != EOF )
printf("%c", c ) ;
int fgetc ( FILE *stream ) ;
22. 22
蠍壱 豢
則 fputc() , putc()
File 覓語襯 磯 .
fputc putc 狩.
char c ;
FILE *fp = fopen("abc.txt", "r" ) ;
FILE *ofp = fopen("xyz.txt", "w" ) ;
while( (c = fgetc(fp)) != EOF )
fputc( c, ofp ) ;
int fputc ( int c, FILE *stream ) ;
23. 23
蠍壱 豢
則 fputs() : Writing a string
殊 puts企.
NULL 覿覿 蟾讌 朱 覲旧.
語 炎概覃 螳 螳 襴危.
語 ろ覃 EOF襯 襴危.
int fputs(char *str, FILE *fp);
24. 24
蠍壱 豢
FILE *fp;
int i;
char *data[3]={"to ben","or notn","to ben"};
fp = fopen("abc.txt", "w");
for(i = 0; i<3; i++) fputs(data[i],fp);
fclose( fp );
25. 25
蠍壱 豢
則 fgets() : Reading a String
殊 gets
fp螳 讌 殊 豕 num 1螳 覓語襯 曙 str
誤誤 覦一伎 ロ.
n企 EOF襯 襷覃 所鍵襯 譴螻 NULL ロ.
所鍵螳 炎概覃 str 襴危螻 蠏碁讌 朱 NULL 襴
.
char *fgets(char *str, int num, FILE *fp);
26. 26
蠍壱 豢
則 fgets()
char s[5] ;
FILE* fp = fopen("abc.txt", "r" ) ;
fgets( s, 5, fp ) ;
123
1 2
1234567
27. 27
蠍壱 豢
則 gets fgets 谿
gets : n螳 り鍵蟾讌 曙. 蠏碁 n
fgets : n螳 り鍵蟾讌 曙. n
28. 28
Accessing a File Randomly
則 殊 覯 所 れ 所 苦朱?
char c ;
FILE *fp = fopen("abc.txt", "r" ) ;
while( !foef(fp) ) {
fscanf( fp, "%c", &c ) ;
printf("%c", c ) ;
}
while( !foef(fp) ) {
fscanf( fp, "%c", &c ) ;
printf("%c", c ) ;
}
abcd
efghi
abc.txt
29. 29
Accessing a File Randomly
則 rewind()
誤磯ゼ 襷
豌朱 蠍企.
void rewind( FILE* ) ;
char c ;
FILE *fp = fopen("abc.txt", r ) ;
while( !foef(fp) ) {
fscanf( fp, "%c", &c ) ;
printf("%c", c ) ;
}
rewind( fp ) ;
while( !foef(fp) ) {
fscanf( fp, "%c", &c ) ;
printf("%c", c ) ;
}
abcd
efghi
abc.txt
30. 30
Accessing a File Randomly
則 fseek()
place offset襷 伎 豺 file position indicator襯
.
place 螳 0 (殊 襷豌 ), 1 (豺), 2(襷) 螳
螳.
fseek( file_ptr, offset, place);
31. 31
Accessing a File Randomly
則 fseek()
fseek( file_ptr, offset, place);
0 (殊 襷豌 )
1 (豺)
2(襷)
0123456789
abcdefghjk
abc.txt
32. 32
Accessing a File Randomly
則 ftell()
file position indicator 螳 return.
return 螳 豌 0朱 螻 豌覿 覈 byte 伎
螳 螳 碁.
朱覿 覓語 曙 襷 indicator 螳 1讀螳
.
int ftell( FILE* );
33. 33
Accessing a File Randomly
則 ftell()
char c ;
int pos ;
FILE *fp = fopen("abc.txt", r ) ;
while( !foef(fp) ) {
fscanf( fp, "%c", &c ) ;
printf("%d: %cn", ftell(fp), c ) ;
}
abcd
efghi
abc.txt
34. 34
stdin, stdout & stderr
則 fprintf襯 伎 覃伎 豢ロ蠍
stdout 覃伎 覩誤 誤一.
則 fscanf襯 伎 る慨 所鍵
stdin る慨襯 覩誤 誤一.
printf( This is a testn ) ; fprintf( stdout, This is a testn ) ;
scanf( %d, &k ) ; fscanf( stdin, %d, &k ) ;
35. 35
stdin, stdout & stderr
Standard C files in stdio.h
Written in C Name Remark
stdin standard input file connected to the keyboard
stdout standard output file connected to the screen
stderr standard error file connected to the screen
則 stdio.h 3螳讌 file pointer
36. 36
stdin, stdout & stderr
#include <stdio.h>
int main() {
int k, j ;
fscanf( stdin, "%d %d", &k, &j ) ;
fprintf( stdout, "Sum: %dn", k + j ) ;
fprintf( stderr, "OKn") ;
return 0 ;
}