1 / 5

Chapter 8

Chapter 8. 파일 입 • 출력. 데이터 파일 C 데이터 파일과 스트림( Stream) 텍스트 파일 처리. C 데이터 파일의 기초. 데이터 파일은 레코드의 집합으로, 보통 외부 기억장치에 저장. 레코드는 연관된 자료들의 집합 . 파일 선언: FILE *outdata; C 프로그램을 이용하여 데이터 파일 작성이 가능하고, 데이터 파일을 읽고 수정하는 것도 가능 파일을 처리하는 함수의 사용법 fopen 함수는 프로그램과 파일 사이의 스트림(통신 채널) 설정

xuxa
Download Presentation

Chapter 8

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. Chapter 8 파일 입•출력 데이터 파일 C 데이터 파일과 스트림(Stream) 텍스트 파일 처리

  2. C 데이터 파일의 기초 • 데이터 파일은 레코드의 집합으로, 보통 외부 기억장치에 저장. 레코드는 연관된 자료들의 집합. • 파일 선언: FILE *outdata; • C 프로그램을 이용하여 데이터 파일 작성이 가능하고, 데이터 파일을 읽고 수정하는 것도 가능 • 파일을 처리하는 함수의 사용법 • fopen 함수는 프로그램과 파일 사이의 스트림(통신 채널) 설정 outdata = fopen(“A:OUTPUT.TXT”, “w”); // “r”, “w”, “a”, “r+”, “w+”, “a+” // 에러 발생 시에는 NULL 리턴 • fscanf, fgetc, fgets, fread 함수는 파일로부터 입력 • fprintf, fputc, fputs, fwrite 함수는 파일에 출력 • feof 함수는 파일의 끝(EOF)을 확인 while(! feof(outdata)) { …} • fclose 함수는 파일을 닫거나 프로그램으로부터 분리 fclose(outdata); • fscanf와fprintf는scanf와printf의 일반형 • scanf(“%d”, &test_score);  fscanf(stdin “%d”, &test_score);

  3. 텍스트 파일 처리(1) • 텍스트 파일에 데이터 출력 • fprintf, fputc, fputs • 예제 8.7 : 정수로 된 텍스트 파일 생성 FILE *out; if ((out = fopen(file_name, “w”)) == NULL) { fprintf(stderr, “***> Open error on output file %s”, file_name); fprintf(out, “%d\n”, test_score); fclose(out); • 예제 8.8 : 문자 텍스트 파일 생성 fputc(ch, out); • 예제 8.9 : 문자열 텍스트 파일 생성 fputs(student_record, out);

  4. 텍스트 파일 처리(2) • 텍스트 파일로부터 데이터 입력 • fscanf, fsetc, fgets • 예제 8.10 : 정수로 된 텍스트 파일(예: A:OUTINT.TXT) 입력 FILE *indata; if ((indata = fopen(file_name, “r”)) == NULL) { fprintf(stderr, “***> Open error reading input file %s”, file_name); fscanf(indata, “%d”, &test_score); while(! feof(indata)) { fclose(indata); • 예제 8.11 : 문자 텍스트 파일 입력 ch = fgetc(in);

  5. 텍스트 파일 처리(3) • 서식화된 데이터 입력 • 파일에서 특정 문자 생략하고 입력 • 입력 파일의 예 1000, 90, 90, 90 1200, 100, 100, 90 100, 45, 55, 78 1400, 67, 77, 48 • 입력을 생략할 문자: ‘,’ • 예제 8.13: 생략될 문자를 읽어 들임 fscanf(test_file, “%4s%c%d%c%d%c%d”, stu_idno, &seperator, &score1, &seperator, &score2, &seperator, &score3); • 예제 8.14: 생략될 문자를 포함 fscanf(test_file, “%4s, %d, %d, %d”, stu_idno, &score1, &score2, &score3); • 예제 8.15: 치환 금지 문자(‘*’) 사용 fscanf(test_file, “%4s%*c%d%*c%d%*c%d”, stu_idno, &score1, &score2, &score3); • 파일 복사: 그림 8.10, 예제 프로그램 1

More Related