1 / 9

17 파일 처리

2007 1 학기. 17 파일 처리. 함수 fopen(). 파일을 만들기 위해서는 함수 fopen() 을 이용 함수 fopen() 의 함수 원형은 다음과 같으며 헤더 파일 stdio.h 파일에 정의 함수 fopen() 은 두 개의 문자열 전달인자를 이용 , 반환 값은 포인터 값인 FILE *. struct _iobuf { char *_ptr; int _cnt; char *_base; int _flag;

hila
Télécharger la présentation

17 파일 처리

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. 2007 1학기 17 파일 처리

  2. 함수 fopen() • 파일을 만들기 위해서는 함수 fopen()을 이용 • 함수 fopen()의 함수 원형은 다음과 같으며 헤더 파일 stdio.h 파일에 정의 • 함수 fopen()은 두 개의 문자열 전달인자를 이용, 반환 값은 포인터 값인 FILE * struct _iobuf { char *_ptr; int _cnt; char *_base; int _flag; int _file; int _charbuf; int _bufsiz; char *_tmpfname; }; typedef struct _iobuf FILE; Perfect C

  3. 파일 열기 • 함수fopen()에서첫번째문자열은처리하려는파일이름이고, 두번째문자열은파일처리종류(모드) • 다음소스는파일 “basic.txt”을여는모듈로서, 파일에자료를쓰기위해파일을열므로모드값을 “w”로기술 • 조건문if를위와같이함수fopen()과함께이용하면파일열기에실패할경우문장"파일이열리지않습니다.\n"을출력 • 파일처리가모두끝났으면파일포인터f를인자로함수fclose(f)를호출하여반드시파일을닫도록 • 함수fopen()의두번째인자는파일처리종류(모드) • “r”, “w”, “a”, “r+”, “w+”, “a+”의종류 FILE *f; char fname[] = "basic.txt"; if ((f = fopen(fname, "w")) == NULL) { printf( "파일이열리지않습니다.\n" ); }; … fclose(f); Perfect C

  4. 파일 처리 모드 • 파일처리모드 종류의미 Perfect C

  5. 표준파일 키워드 장치(device) 표준입력 stdin 키보드 표준출력 stdout 모니터화면 표준에러 stderr 모니터화면 함수 fprint, fscanf • 파일에자료를쓰거나읽기위하여함수fprintf()와fscanf()를이용 • 이함수를이용하기위해서는헤더파일stdio.h 파일을포함 • 함수fprintf()와fscanf()의함수원형 • 위함수의첫번째인자는입출력에이용될파일이고, 두번째인자는입출력되는문자열이며, 다음인자들은입출력될변수목록 • 함수원형에서기호…은여러인자가계속됨을의미 • 함수fprintf()와fscanf()를표준입출력에도이용 가능 • 즉함수fprintf()와fscanf()의첫번째인자에각각stdin 또는stdout를이용하면표준입력, 표준출력으로이용이가능 • 기호상수stdin, stdout은stderr과함께헤더파일stdio.h에정의되어있는값으로각각표준입력, 표준출력, 표준에러를의미 int fprintf(FILE *, const char *, ...); int fscanf(FILE *, const char *, ...); #define stdin (&_iob[0]) #define stdout (&_iob[1]) #define stderr (&_iob[2]) Perfect C

  6. 함수 fgets()와fputs() • 함수 fgets()와fputs() • 파일에 문자열을 입출력하는 함수로 fgets()와fputs() • 이함수도헤더파일stdio.h 파일에다음과같은함수원형으로정의 • 함수 fgets()는 문자열을 개행문자(\n)까지 읽어 개행문자도 함께 입력 문자열에 저장 • 마찬가지로함수 fputs()는 문자열을 그대로 출력 • 함수 fgets() 인자 • 첫 번째 인자는 문자열이 저장될 문자 포인터이고, • 두 번째 인자는 입력할 문자의 최대 수이며, • 세 번째 인자는 입력 문자열이 저장될 파일 • 함수 fputs() 인자 • 첫 번째 인자는 출력될 문자열이 저장된 문자 포인터이고, • 두 번째 인자는 문자열이 출력되는 파일 char * fgets(char *, int, FILE *); int fputs(char *, FILE *); Perfect C

  7. 함수fgetc()와fputc() • 문자 하나를 파일에 입출력하는 함수로 fgetc()와 fputc()를 제공 • 이 함수의 원형은 헤더파일 stdio.h에 정의 • 이함수들은문자하나의입출력의대상인파일포인터를인자로이용 • 이함수와같은함수로getc()와putc()도이용가능 • 문자의표준입출력에이용되는getchar()와putchar()는다음과같이함수getc()와putc()를이용한매크로 • 함수fgetc()와 fputc()는 getc()와putc()와그기능은동일하며, fgetc()와 fputc()는 함수이고, getc()와putc()는매크로 int fgetc(FILE *); int fputc(int, FILE *); int getc(FILE *); int putc(int, FILE *); #define getchar() getc(stdin) #define putchar(_c) putc((_c),stdout) Perfect C

  8. 함수feof()와ferror() • 함수feof() • 파일의내부포인터위치가파일의끝(EOF)인지를검사하는데필요한함수 • 이함수는헤더파일stdio.h에다음함수원형으로정의 • 파일의위치가파일의마지막(end of file)인지를검사하여, 파일의마지막이면0이아닌값을, 파일의마지막이아니면0을반환 • 그러므로표준입력에서계속적으로입력을받는구문으로다음을이용 가능 • 함수ferror() • 파일처리에서오류가발생했는지검사하는함수 • 함수의원형은헤더파일stdio.h에정의 • 이전파일처리에서오류가발생하면0이아닌값을발생하고, 오류가발생하지않으면0을반환 int feof(FILE *); while (!feof(stdin)) { … } int ferror(FILE *); Perfect C

  9. 실습(17-3) if ( (f = fopen(fname, “r”)) == NULL ) { printf(“error!! \n”); exit(1); }; printhead(); while( !feof(f) ) { fscanf(f, “%d %s %d %d\n”, &cnt, names, &point1, &point2); fprintf(stdout, “%6d%18s%8d%8d\n”, cnt, names, point1, point2); } fprintf(stdout, “_____________”); fclose(f); return 0; void printhead() { fprintf(stdout, %6s%18s%8s%8s\n”, ”번호”, “이름”,”중간”, “기말”); } #include <stdio.h> #include <stdlib.h> Void printhead(); Int main() { int point1, point2, cnt=0; char fname[] =“grade.txt”; char names[80]; FILE *f; if ( (f = fopen(fname, “w”)) == NULL ) { printf(“error!! \n”); exit(1); }; printf(“이름과 성적(중간, 기말) 입력.\n”); fgets(names, 80, stdin); while( !feof(stdin) ) { fprintf(f, “%d”, ++cnt); fputs(names, f); fgets(names, 80, stdin); } fclose(f); Perfect C

More Related