1 / 13

파일 입출력

파일 입출력. 서울호서전문학교 김 욱. 1. 파일의 열기와 닫기. 파일 개방 함수 fopen. 1. 파일의 열기와 닫기. 파일 접근 방식. 1. 파일의 열기와 닫기. 파일 종료 함수 fclose. 1. 파일의 열기와 닫기. #include <stdio.h> exam.txt 가 없을시 void main() {

Download Presentation

파일 입출력

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 파일 입출력 서울호서전문학교 김 욱

  2. 1. 파일의 열기와 닫기 파일 개방 함수 fopen

  3. 1. 파일의 열기와 닫기 파일 접근 방식

  4. 1. 파일의 열기와 닫기 파일 종료 함수 fclose

  5. 1. 파일의 열기와 닫기 #include <stdio.h> exam.txt 가 없을시 void main() { FILE *fp; exam.txt 가 존재 fp = fopen(“exam.txt”, "r”); ② if(fp==NULL) ③ printf(" file not found!\n”); else { printf(“file opened!\n”); fclose(fp); ④ printf(“file closed!\n”); } } ① 프로그램 설명 ① 파일포인터 fp를 선언 ② exam.txt를 읽기 전용으로 개방 ③ exam.txt 파일이 없을시 “file not found!” 출력 ④ 파일을 닫음

  6. 2. 파일의 읽기와 쓰기 파일 읽기 함수 fgetc 파일 쓰기 함수 fputc

  7. 3. fgetc()를 이용한 파일 읽기 #include <stdio.h> void main() { FILE *fp; char c; fp = fopen("exam.txt", "r"); if(fp == NULL) printf("file not found!\n"); else{ printf("exam.txt: \n"); while((c = fgetc(fp)) != EOF) printf("%c", c); fclose(fp); } } ① ② 프로그램 설명 ① 파일로 부터 하나씩 읽어들인 문자가 파일의 끝(End Of File) 인지 검사 ② 파일로 부터 읽어들인 문자를 화면에 출력

  8. 4. fputc()를 이용한 파일 쓰기 #include <stdio.h> void main() { FILE *fpr, *fpw; char c; fpr = fopen("exam.txt", "r"); fpw = fopen("test.txt", "w"); if(fpr == NULL) printf("file not found!\n"); else{ while((c = fgetc(fpr)) != EOF) fputc(c, fpw); fclose(fpr); fclose(fpw); } } test.txt 의 내용 ① ② 프로그램 설명 ① exam.txt를 쓰기 전용 모드로 개방 ② 파일로 부터 읽어들인 문자를 test.txt에 기록

  9. 5. 추가(Append)모드를 통한 파일 내용 추가 #include <stdio.h> void main() { FILE *fp; char c; fp = fopen("exam.txt", "a"); while((c = getchar()) != EOF) fputc(c, fp); fclose(fp); } CTRL + z exam.txt 의 내용 ① ② ③ 프로그램 설명 ① exam.txt를 추가모드로 개방 ② 키보드로 부터 내용을 한글자씩 읽어들임 ③ 읽어들인 내용을 exam.txt 파일 끝에 추가

  10. 6. fscanf를 이용한 파일 입력 #include <stdio.h> void main() { FILE *fp; int i, a[5]; fp = fopen("number.txt", "r"); if(fp == NULL) printf("file not found!\n"); else{ for(i=0;i<5;i++) fscanf(fp, "%d", &a[i]); fclose(fp); } for(i=0;i<5;i++) printf("%d ",a[i]); } number.txt 의 내용 출력 결과 ① ② 프로그램 설명 ① number.txt를 읽기 전용으로 개방 ② 파일로 부터 정수를 읽어들여 배열 a에 저장

  11. 7. fprintf를 이용한 파일 출력 #include <stdio.h> void main() { FILE *fp; int i, a[5]; fp = fopen("result.txt", "w"); for(i=0;i<5;i++) a[i] = i * 2; for(i=0;i<5;i++) fprintf(fp, "%d ", a[i]); fclose(fp); } result.txt 의 내용 ① ② 프로그램 설명 ① result.txt 파일을 쓰기 전용으로 개방 ② result.txt 파일에 배열 a의 각 요소를 저장

  12. 실습 파일 입출력 프로그램 1 ~ 100 까지의 정수를 out.txt 파일에 저장하시오 fopen, fprintf, fclose함수 이용 생성된 out.txt로 부터 100개의 정수를 읽어들여 총합과평균을 구하시오 fopen, fscanf, fclose 함수 이용 총합과 평균을 result.txt로 저장하시오 fopen, fprintf, fclose 함수 이용

  13. 실습 out.txt • result.txt

More Related