9 Hello!! C
printf() scanf() getchar() putchar() gets() puts() fopen() fclose() fprintf() fscant() fgetc() fputs() fgets() gputs() fread() fwrite() fseek() ftell() I/O 2
(stream) C (text stream) : `/n' (binary stream) : (file) open close exit() 3
C stdin standard input file 0 stdout standard output file 1 stderr standard error file 2 "stdioh" 4
printf() scanf() printf() print formatted printf() printf(, ); 5
printf() scanf() %d : int %ld : long %x : int 16 %lx : long 16 %o : int 8 %lo : long 8 %u : unsigned %lu : long unsigned %e : %f : %g : %e %f %p : %c : %s : 6
printf() scanf() [ 9-1] printf("%5d", a); [ ] 1234567 7
printf() scanf() [ 9-2] #include <stdioh> main() { float a, b; a = 150; b = 30; printf(" = %82f\n", a+b); printf(" = %82f\n", a-b); printf(" = %82f\n", a*b); printf(" = %82f\n", a/b); } [ ] = 1800 = 1200 = 4500 = 500 8
printf() scanf() scanf() scanf() printf() (call by reference) & scanf(, ); 9-3, 9-4 9
printf() scanf() [ 9-3] #include <stdioh> main() { int a, b; float average; } printf("input data = "); scanf("%d %d", &a, &b); average = (a+b)/2; printf("average of two numbers = %f\n",average); [ ] input data = 10, 20 average of two numbers = 15000000 10
printf() scanf() [ 9-4] #include <stdioh> main() { double x, y, average = 00; printf("input data(x, y) = "); scanf("%f %f", &x, &y); printf("sum of two numbers = %f\n", x+y); printf("average of two numbers = %f\n", (x+y)/2); } [ ] input data(x, y) = 100, 200 sum of two numbers = 30000000 average of two numbers = 15000000 11
getchar() putchar() getchar() putchar() char ch; ch = getchar(); putchar(ch); 12
getchar() putchar() [ 9-5] #include <stdioh> main() { char str; str = getchar(); putchar(str); putchar('\n'); putchar('a'); } [ ] d d A 13
getchar() putchar() [ 9-6] #include <stdioh> main() { char str; str = getchar(); while(str!= EOF) putchar(str); } [ ] example example ABCD EFG ABCD EFG 1234567 1234567 ^Z 14
gets() puts() gets() scanf() scanf() <Enter>, gets() <Entert> : + 1 NULL puts() NULL char name[10]; gets(name); puts(name); 15
gets() puts() [ 9-7] #include <stdioh> main() { char name[15], irum[15]; gets(name); scanf("%s", irum); printf("name = %s\n", name); printf("irum = %s\n", irum); } [ ] name = Hong Gil Dong irum = Hong 16
gets() puts() [ 9-8] #include <stdioh> main() { char ch[15]; gets(ch); printf("%s", ch); puts(ch); } [ ] Hong Gil DongHong Gil Dong 17
: "datain : "dataout : "mainc 3 18
19 fopen() fclose() fscanf() fprintf() fgetc() fputc() fgets() fputs()
fopen() fclose() fopen() filename mode FILE *fopen(filename, mode) constchar *filename; constchar *mode; / * filename : */ /* mode : */ 20
fopen() fclose() (mode) "r" "w" "a" "r+" ( ) "w+" "a+", "rb" "wb" "ab" "rb+" "wb+" "ab+", 21
fopen() fclose() [ 9-9] FILE *in, *out; in = fopen("examplein", "r"); out = fopen("exampleout", "w"); 22
fopen() fclose() fclose() :, int fclose(fptr); FILE *fptr; /* fptr : */ exit() 23
fopen() fclose() [ 9-10] #include <stdioh> main() { FILE *in, *out; } /* */ /* */ in = fopen("examplein", "r"); out = fopen("exampleout", "w"); fclose(in); fclose(out); /* */ 24
fprintf() fscant() fprintf() : printf() int fprintf(fptr, format, argument list); FILE *fptr; constchar *format; /* fptr : */ /* format : */ printf() fprintf(stdout, format, argument list); printf(format, argument list); 25
fprintf() fscant() fscanf() : scanf() int fscanf(fptr, format, argument list); FILE *fptr; const char *format; /* fptr : */ /* format : */ scanf() fscanf(stdin, format, argument list); scanf(format, argument list); 26
fprintf() fscant() [ 9-11] #include <stdioh> #include <stdlibh> main() { int kor, eng, mat, total; float average; FILE *in, *out; in = fopen("examplein", "r"); out = fopen("exampleout", "w"); if( in == NULL){ printf("file not found!!!"); exit(1); } 27
fprintf() fscant() [ 9-11] } fprintf(out, " *********** *********** \n"); fprintf(out, " \n\n"); while( fscanf(in, "%d %d %d", &kor, &eng, &mat)!= EOF ){ total = kor + eng + mat; average = total/3; fprintf(out, "%6d %8d %9d %82f\n", kor, eng,mat, total, average); } fclose(in); fclose(out); 28
fprintf() fscant() [ 9-11] [ ] *********** *********** 80 80 80 240 8000 90 95 85 270 9000 70 75 65 210 7000 90 75 100 265 8833 29
fgetc() fputc() fgetc() : int int fgetc(fptr); FILE *fptr; /* fptr : */ 30
fgetc() fputc() fputc() : int fputc(ch, fptr); FILE *fptr; 9-12 char ch; /* fptr : */ /* ch : */ fgetc() fputc() 31
fgetc() fputc() [ 9-12] #include <stdioh> #include <stdlibh> main(){ char ch; FILE *in, *out; in = fopen("examplec", "r"); if(in== NULL) { printf("file not found!!!\n") exit(1) } out = fopen("copyc", "w"); while((ch = fgetc(in))!= EOF) fputc(ch, out); [ ] fclose(in); examplec" fclose(out); copyc" } 32
fgets() fputs() fgets() : s ( \n ) EOF NULL char *fgets(s, n, fptr); char *s; int n; FILE *fptr; /* s : */ /* n : */ /* fptr : */ 33
fgets() fputs() fputs() : s int fputs(s, fptr); char *s; FILE *fptr; /* s : */ /* fptr : */ 34
fgets() fputs() [ 9-13] /* filename : inputc */ The limits in your life are the size of your ideas and your of determination degree #include <stdioh> main(){ char buf[256]; FILE *in, *out; int cnt = 0; in = fopen("inputc", "r"); out = fopen("outputc", "w"); while(fgets(buf, 256, in)!= NULL) { fputs(buf, out); ++cnt; } 35
fgets() fputs() [ 9-13] printf("this file includes %d lines\n", cnt); fclose(in); fclose(out); } [ ] This file includes 2 lines, "outputc" inputc" 36
fread() fwrite() fread() : n cnt buf int fread(buf, n, cnt, fptr); void *buf; int n; int cnt; FILE *fptr; /* buf : */ /* n : */ /* cnt : */ /* fptr : */ 37
fread() fwrite() fwrite() : int fwrite(buf, n, cnt, fptr); char *buf; int n; int cnt; FILE *fptr; /* buf : */ /* n : */ /* cnt : */ /* fptr : */ 38
fread() fwrite() [ 9-14] #include <stdioh> main() { FILE *fp; float f = 1005; if((fp = fopen("testc", "wb")) == NULL) { printf("cannot open file!\n"); return; } fwrite(&f, sizeof(float), 1, fp); fclose(fp); } 39
fseek() ftell() fseek() int fseek(fptr, offset, whence); FILE *fptr; long int offset; int whence; /* fptr : */ /* offset : */ /* whence : offset */ 40
fseek() ftell() Fseek() :, offset whence offset long whence 0 : (SEEK_SET) 1 : (SEEK_CUR) 2 : (SEEK_END) 41
fseek() ftell() ftell() long int ftell(fptr); FILE *fptr; /* fptr : */ 9-15,, 42
fseek() ftell() [ 9-15] #include <stdioh> main() { long int num, score, size = 16L; static char name[11], ch; FILE *out; out = fopen("randomdat", "w"); while(1){ printf("\n, ( 10 ), \n"); scanf("%1d %10s %3d", &num, &name, &score); fseek(out, (num-1)*size, 0); fprintf(out, "%3d %10s %3d", num, name, score); printf("? (Y/N)"); ch = getchar(); 43
fseek() ftell() [ 9-15] if((ch == 'Y' ch == 'y') continue; else break; } fclose(out); } [ ] 4 80 2 95 1 72 3 98 [ ] randomdat 1 72 2 95 3 98 4 80 44