1. 스트림 - 입출력을 소프트웨어로 구현
2. 표준 입출력 스트림
stdin - 표준입력
stdout - 표준출력
stderr - 표준에러
3. 파일 오픈/읽기/쓰기
1)파일 오픈
FILE *fp = fopen("파일경로", "오픈모드");
오픈모드: r(읽기), w(쓰기), a(이어쓰기)
fopen()은 오픈한 파일의 정보를 담은 FILE 구조체의 주소를 반환.
NULL을 반환하면 오픈에러.
FILE *fp = fopen("a.txt", "r");
fclose(fp)=> 오픈한 파일을 닫음
| #include <stdio.h> void main(){ FILE *fp = fopen("a.txt", "r"); if(fp==NULL){ printf("file open error\n"); return; }else{ printf("파일 오픈됨"); } fclose(fp);//파일 닫는다 } |
2) 한문자씩 읽기
fgetc(fp): 스트림에서 문자 하나 읽어 반환
getc(fp): 스트림에서 문자 하나 읽어 반환
getchar(): 표준 입력 스트림에서 문자 하나 읽어 반환
| #include <stdio.h> void main(){ int ch; FILE *fp = fopen("a.txt", "r"); if(fp==NULL){ printf("file open error\n"); return; } while((ch=fgetc(fp))!=-1){//EOF:-1 printf("%c", (char)ch); } /* while((ch=getc(fp))!=-1){ printf("%c", (char)ch); } */ fclose(fp);//파일 닫는다 printf("\n"); puts("문자열 하나를 입력하라"); while((ch=getchar())!='\n'){ printf("%c", (char)ch); } printf("\n"); puts("문자열 하나를 입력하라"); while((ch=fgetc(stdin))!='\n'){ printf("%c", (char)ch); } } |
3) 한문자씩 쓰기
fputc(ch, fp): 스트림에 문자 하나(ch) 씀
putc(ch, fp): 스트림에 문자 하나(ch) 씀
| #include <stdio.h> void main(){ char msg[]="hello"; int i; FILE *fp = fopen("b.txt", "w"); if(fp==NULL){ printf("file open error"); return; } for(i=0;i<5;i++){ fputc(msg[i], fp); } fclose(fp); } |
| 파일복사 |
| #include <stdio.h> void main(){ FILE *fp1 = fopen("a.txt", "r"); FILE *fp2 = fopen("b.txt", "w"); int ch; if(fp1==NULL || fp2==NULL){ printf("file open error"); return; } while((ch=fgetc(fp1))!=-1){ fputc(ch, fp2); } fclose(fp1); fclose(fp2); } |
4) 한줄씩 읽기
fgets(buf, size, fp): fp스트림에서 한 줄(size만큼) 읽어서 buf에 저장
gets(buf): 표준 입력스트림에서 한 줄 읽어서 buf에 저장
| #include <stdio.h> void main(){ FILE *fp = fopen("a.txt", "r"); char buf[256]; if(fp==NULL){ printf("file open error"); return; } while(fgets(buf, 256, fp)!=NULL){ printf("%s\n", buf); } fclose(fp); } |
5)한줄 출력
fputs(buf, fp): buf 한줄을 fp 스트림에 쓴다
puts(buf): buf 한줄을 표준출력스트림에 쓴다. 끝에 자동 개행
| #include <stdio.h> void main(){ char buf[][20]={"hello", "banana", "apple", "melon"}; int i; char buf2[20]; FILE *fp = fopen("b.txt", "w"); if(fp==NULL){ printf("file open error"); return; } for(i=0;i<4;i++){ fputs(buf[i], fp); fputc('\n', fp); } fclose(fp); fp = fopen("b.txt", "r"); if(fp==NULL){ printf("file open error"); return; } while(fgets(buf2, 20, fp)!=NULL){ puts(buf2); } fclose(fp); } |
6)위치제어
ftell(fp):스트림에서 현재 읽고 쓸 위치 반환
fseek(fp, off, whence): 스트림의 읽고 쓸 위치를 whence를 기준으로 off만큼 이동한 위치로 변경
| #include <stdio.h> void main(){ int ch, i; FILE *fp = fopen("b.txt", "r"); if(fp==NULL){ printf("file open error"); return; } for(i=0;i<3;i++){ ch = fgetc(fp); printf("%c", (char)ch); } printf("\n"); printf("현재 위치 %d\n", ftell(fp)); fseek(fp, -1 , SEEK_CUR); ch = fgetc(fp); printf("현재 위치 한 칸 앞=%c\n", (char)ch); fseek(fp, 8 , SEEK_SET); ch = fgetc(fp); printf("맨 앞에서 뒤로 8칸 이동한 위치=%c\n", (char)ch); fseek(fp, -3 , SEEK_END); ch = fgetc(fp); printf("맨 뒤에서 앞으로 3칸 이동한 위치=%c\n", (char)ch); fclose(fp); } |
7) 형식화된 입출력 함수
*형식화된 출력함수
printf(char *fmt, ...):표준 출력에 형식화된 출력
fprintf(fp, char *fmt, ...):오픈한 스트림에 형식화된 출력
sprintf(char*, char *fmt, ...):메모리에 형식화된 출력
*형식화된 읽기
scanf(char *fmt, ...): 표준 입력에서 형식화된 읽기
fscanf(fp, char *fmt, ...): 오픈한 스트림에 형식화된 읽기
sscanf(char*, char *fmt, ...):메모리에 형식화된 읽기
| fprintf(), fscanf() 예제 |
| #include <stdio.h> void main(){ FILE *fp = fopen("a.txt", "w"); char name[]="oh_nami"; int age = 23; float height = 63.5f; fprintf(fp, "%s\n", name); fprintf(fp, "%d\n", age); fprintf(fp, "%f\n", height); fclose(fp); char name2[20]; int age2; float height2; fp = fopen("a.txt", "r"); fscanf(fp, "%s%d%f", name2, &age2, &height2); printf("name2:%s / age2:%d / height2:%f", name2, age2, height2); fclose(fp); } |
| sprintf(), sscanf() 예제 |
| #include <stdio.h> void main(){ char buf[30]; char name[]="oh_nami"; int age = 23; float height = 63.5f; sprintf(buf, "%s %d %f", name, age, height); printf("buf=%s\n", buf); char name2[20]; int age2; float height2; sscanf(buf, "%s%d%f", name2, &age2, &height2); printf("name2:%s / age2:%d / height2:%f", name2, age2, height2); } |
| sprintf(), sscanf() 활용 |
| #include <stdio.h> void main(){ char buf[5][10]; int i; for(i=0;i<5;i++){ sprintf(buf[i], "%s%d", "msg", (i+1)); } for(i=0;i<5;i++){ printf("%s\n", buf[i]); } } |
댓글