a 001 002 003 004 005 006 007

  • Slides: 40
Download presentation

예제 : "a" /*001*/ /*002*/ /*003*/ /*004*/ /*005*/ /*006*/ /*007*/ /*008*/ /*009*/ /*010*/ /*011*/

예제 : "a" /*001*/ /*002*/ /*003*/ /*004*/ /*005*/ /*006*/ /*007*/ /*008*/ /*009*/ /*010*/ /*011*/ /*012*/ /*013*/ /*014*/ /*015*/ /*016*/ /*017*/ SQ Lab. /* fopen 01 a 00 */ #include "stdio. h" #pragma warning(push, 2) void main() { FILE *a. File; a. File = fopen("c: \files\fopen 01 a 00. txt", "a"); if(a. File == NULL) { printf("n open() error"); getchar(); return; } 5

/*018*/ char data 1[] = "99999 z 123456789_123456789010 -9999n"; /*019*/ char data 2[] =

/*018*/ char data 1[] = "99999 z 123456789_123456789010 -9999n"; /*019*/ char data 2[] = "88888 y 123456789_123456789010 -8888n"; /*020*/ char data 3[] = "77777 x 123456789_123456789010 -7777_7777n"; /*021*/ /*022*/ fwrite(data 1, 44, 1, a. File); /*023*/ fseek(a. File, 0, SEEK_SET); /*024*/ fwrite(data 2, 44, 1, a. File); /*025*/ fseek(a. File, 0, SEEK_SET); /*026*/ fwrite(data 3, 44, 1, a. File); /*027*/ /*028*/ fclose(a. File); /*029*/ /*030*/ printf("nn 끝났습니다(^@^), <Enter>하십시요. "); /*031*/ getchar(); /*032*/ } SQ Lab. 6

예제 : "r+" /*001*/ /*002*/ /*003*/ /*004*/ /*005*/ /*006*/ /*007*/ /*008*/ /*009*/ /*010*/ /*011*/

예제 : "r+" /*001*/ /*002*/ /*003*/ /*004*/ /*005*/ /*006*/ /*007*/ /*008*/ /*009*/ /*010*/ /*011*/ /*012*/ /*013*/ /*014*/ /*015*/ /*016*/ /*017*/ SQ Lab. /* fopen 01 rplus 00 */ #include "stdio. h" #pragma warning(push, 2) void main() { FILE *rplus. File; rplus. File = fopen("c: \files\fopen 01 rplus 00. txt", "r+"); if(rplus. File == NULL) { printf("n open() error"); getchar(); return; } 9

/*018*/ /*019*/ /*020*/ /*021*/ /*022*/ /*023*/ /*024*/ /*025*/ /*026*/ /*027*/ /*028*/ /*029*/ /*030*/ /*031*/

/*018*/ /*019*/ /*020*/ /*021*/ /*022*/ /*023*/ /*024*/ /*025*/ /*026*/ /*027*/ /*028*/ /*029*/ /*030*/ /*031*/ /*032*/ /*033*/ /*034*/ /*035*/ /*036*/ /*037*/ /*038*/ SQ Lab. char io. Buffer[45]; fread(io. Buffer, 44, 1, rplus. File); io. Buffer[44] = ''; printf("n%s", io. Buffer); char data 1[] = "33333 z 123456789_123456789010 -3333n"; char data 2[] = "44444 d 123456789_123456789010 -4444n"; fseek(rplus. File, ftell(rplus. File)+45, SEEK_SET); fwrite(data 1, 44, 1, rplus. File); fseek(rplus. File, ftell(rplus. File), SEEK_SET); fwrite(data 2, 44, 1, rplus. File); fseek(rplus. File, ftell(rplus. File), SEEK_SET); fread(io. Buffer, 44, 1, rplus. File); io. Buffer[44] = ''; printf("n%s", io. Buffer); 10

/*027*/ fclose(rplus. File); /*028*/ printf("nn 끝났습니다(^@^), <Enter>하십시요. "); /*029*/ getchar(); /*030*/ } SQ Lab.

/*027*/ fclose(rplus. File); /*028*/ printf("nn 끝났습니다(^@^), <Enter>하십시요. "); /*029*/ getchar(); /*030*/ } SQ Lab. 11

예제 : "w+" /*001*/ /*002*/ /*003*/ /*004*/ /*005*/ /*006*/ /*007*/ /*008*/ /*009*/ /*010*/ /*011*/

예제 : "w+" /*001*/ /*002*/ /*003*/ /*004*/ /*005*/ /*006*/ /*007*/ /*008*/ /*009*/ /*010*/ /*011*/ /*012*/ /*013*/ /*014*/ /*015*/ /*016*/ /*017*/ SQ Lab. /* fopen 01 wplus 00 */ #include "stdio. h" #pragma warning(push, 2) void main() { FILE *wplus. File; wplus. File = fopen("c: \files\fopen 01 wplus 00. txt", "w+"); if(wplus. File == NULL) { printf("n open() error"); getchar(); return; } 15

/*018*/ /*019*/ /*020*/ /*021*/ /*022*/ /*023*/ /*024*/ /*025*/ /*026*/ /*027*/ /*028*/ /*029*/ /*030*/ /*031*/

/*018*/ /*019*/ /*020*/ /*021*/ /*022*/ /*023*/ /*024*/ /*025*/ /*026*/ /*027*/ /*028*/ /*029*/ /*030*/ /*031*/ /*032*/ /*033*/ /*034*/ /*035*/ /*036*/ /*037*/ SQ Lab. char data 1[] = "99999 z 123456789_123456789010 -9999n"; char data 2[] = "88888 y 123456789_123456789010 -8888n"; char data 3[] = "77777 x 123456789_123456789010 -7777n"; fwrite(data 1, 44, 1, wplus. File); fwrite(data 2, 44, 1, wplus. File); fwrite(data 3, 44, 1, wplus. File); fseek(wplus. File, 0, SEEK_SET); char io. Buffer[45]; do { fread(io. Buffer, 44, 1, wplus. File); if(feof(wplus. File)) break; io. Buffer[44] = ''; printf("n%s", io. Buffer); }while(1); fclose(wplus. File); 16

/*038*/ /*039*/ printf("nn 끝났습니다(^@^), <Enter>하십시요. "); /*040*/ getchar(); /*041*/ } SQ Lab. 17

/*038*/ /*039*/ printf("nn 끝났습니다(^@^), <Enter>하십시요. "); /*040*/ getchar(); /*041*/ } SQ Lab. 17

예제 : "a+" /*001*/ /*002*/ /*003*/ /*004*/ /*005*/ /*006*/ /*007*/ /*008*/ /*009*/ /*010*/ /*011*/

예제 : "a+" /*001*/ /*002*/ /*003*/ /*004*/ /*005*/ /*006*/ /*007*/ /*008*/ /*009*/ /*010*/ /*011*/ /*012*/ /*013*/ /*014*/ /*015*/ /*016*/ /*017*/ SQ Lab. /* fopen 01 aplus 00 */ #include "stdio. h" #pragma warning(push, 2) void main() { FILE *aplus. File; aplus. File = fopen("c: \files\fopen 01 aplus 00. txt", "a+"); if(aplus. File == NULL) { printf("n open() error"); getchar(); return; } 20

/*018*/ /*019*/ /*020*/ /*021*/ /*022*/ /*023*/ /*024*/ /*025*/ /*026*/ /*027*/ /*028*/ /*029*/ /*030*/ /*031*/

/*018*/ /*019*/ /*020*/ /*021*/ /*022*/ /*023*/ /*024*/ /*025*/ /*026*/ /*027*/ /*028*/ /*029*/ /*030*/ /*031*/ /*032*/ /*033*/ /*034*/ /*035*/ /*036*/ /*037*/ SQ Lab. char data 1[] = "99999 z 123456789_123456789010 -9999n"; char data 2[] = "88888 y 123456789_123456789010 -8888n"; char data 3[] = "77777 x 123456789_123456789010 -7777n"; fwrite(data 1, 44, 1, aplus. File); fwrite(data 2, 44, 1, aplus. File); fwrite(data 3, 44, 1, aplus. File); fseek(aplus. File, 0, SEEK_SET); char io. Buffer[45]; do { fread(io. Buffer, 44, 1, aplus. File); if(feof(aplus. File)) break; io. Buffer[44] = ''; printf("n%s", io. Buffer); }while(1); fclose(aplus. File); 21

/*038*/ /*039*/ printf("nn 끝났습니다(^@^), <Enter>하십시요. "); /*040*/ getchar(); /*041*/ } SQ Lab. 22

/*038*/ /*039*/ printf("nn 끝났습니다(^@^), <Enter>하십시요. "); /*040*/ getchar(); /*041*/ } SQ Lab. 22

예제 : "b" /*001*/ /*002*/ /*003*/ /*004*/ /*005*/ /*006*/ /*007*/ /*008*/ /*009*/ /*010*/ /*011*/

예제 : "b" /*001*/ /*002*/ /*003*/ /*004*/ /*005*/ /*006*/ /*007*/ /*008*/ /*009*/ /*010*/ /*011*/ /*012*/ /*013*/ /*014*/ /*015*/ /*016*/ /*017*/ SQ Lab. /* fopen 01 b 00 */ #include "stdio. h" #pragma warning(push, 2) void main() { FILE *b. File; b. File = fopen("c: \files\fopen 01 b 00. txt", "w+b"); if(b. File == NULL) { printf("n open() error"); getchar(); return; } 26

/*018*/ /*019*/ /*020*/ /*021*/ /*022*/ /*023*/ /*024*/ /*025*/ /*026*/ /*027*/ /*028*/ /*029*/ /*030*/ /*031*/

/*018*/ /*019*/ /*020*/ /*021*/ /*022*/ /*023*/ /*024*/ /*025*/ /*026*/ /*027*/ /*028*/ /*029*/ /*030*/ /*031*/ /*032*/ /*033*/ /*034*/ /*035*/ /*036*/ /*037*/ SQ Lab. char data 1[] = "11111 a 123456789_123456789010 -1111n"; char data 2[] = "22222 b 123456789_123456789010 -2222n"; char data 3[] = "33333 c 123456789_123456789010 -3333n"; fwrite(data 1, 44, 1, b. File); /*‘n’은 CR-LF로 치환되지 않고 */ fwrite(data 2, 44, 1, b. File); /* 코드로 그대로 수록 */ fwrite(data 3, 44, 1, b. File); fseek(b. File, 0, SEEK_SET); char io. Buffer[45]; do { fread(io. Buffer, 44, 1, b. File); if(feof(b. File)) break; io. Buffer[44] = ''; printf("n%s", io. Buffer); }while(1); fclose(b. File); 27

/*038*/ /*039*/ printf("nn 끝났습니다(^@^), <Enter>하십시요. "); /*040*/ getchar(); /*041*/ } SQ Lab. 28

/*038*/ /*039*/ printf("nn 끝났습니다(^@^), <Enter>하십시요. "); /*040*/ getchar(); /*041*/ } SQ Lab. 28

■실행 후 파일 : c₩files₩fopen 01 rplus 02. txt SQ Lab. 34

■실행 후 파일 : c₩files₩fopen 01 rplus 02. txt SQ Lab. 34

[실습 program] fopen 01 rplus 02 /*001*/ /*002*/ /*003*/ /*004*/ /*005*/ /*006*/ /*007*/ /*008*/

[실습 program] fopen 01 rplus 02 /*001*/ /*002*/ /*003*/ /*004*/ /*005*/ /*006*/ /*007*/ /*008*/ /*009*/ /*010*/ /*011*/ /*012*/ /*013*/ /*014*/ /*015*/ /*016*/ SQ Lab. /* fopen 01 rplus 02 */ #include "stdio. h" #include "conio. h" #include "windows. h" #pragma warning(push, 2) void goto. XY(int X, int Y) { COORD cursor; cursor. X = X; cursor. Y = Y; Set. Console. Cursor. Position(Get. Std. Handle(STD_OUTPUT_HANDLE), cursor); } 35

/*017*/ void input. Function(FILE* io. File, int *point) /*018*/ { /*019*/ char number[11]; /*020*/

/*017*/ void input. Function(FILE* io. File, int *point) /*018*/ { /*019*/ char number[11]; /*020*/ char name[21]; /*021*/ char hp[14]; /*022*/ /*023*/ do /*024*/ { /*025*/ fseek(io. File, *point, SEEK_SET); /*026*/ /*027*/ fread(number, 10, 1, io. File); /*028*/ if(feof(io. File)) /*029*/ { /*030*/ system("cls"); /*031*/ printf("n 더 이상 입력자료가 없습니다. " /*032*/ " 파일의 처음으로 이동합니다. "); /*033*/ *point = 0; /*034*/ fseek(io. File, *point, SEEK_SET); /*035*/ fread(number, 10, 1, io. File); /*036*/ if(feof(io. File)) printf("r 입력자료가 없습니다. " /*037*/ " 자료 수록 후 입력 가능합니다. " /*038*/ " "); /*039*/ getche(); /*040*/ return; /*041*/ } /*042*/ number[10] = ''; SQ Lab. 36

/*043*/ fread(name, 20, 1, io. File); /*044*/ name[20] = '�'; /*045*/ fread(hp, 14, 1,

/*043*/ fread(name, 20, 1, io. File); /*044*/ name[20] = ''; /*045*/ fread(hp, 14, 1, io. File); /*046*/ hp[13] = ''; /*047*/ /*048*/ *point =ftell(io. File); /*049*/ /*050*/ system("cls"); /*051*/ goto. XY(1, 1); puts("number : "); /*052*/ goto. XY(25, 1); puts("name : "); /*053*/ goto. XY(27, 3); puts("hp : "); /*054*/ /*055*/ goto. XY(10, 1); puts(number); /*056*/ goto. XY(32, 1); puts(name); /*057*/ goto. XY(32, 3); puts(hp); /*058*/ /*059*/ printf("nnn 다음 자료 확인=<Enter> 아니면=<Enter>이외문자"); /*060*/ number[0] = getche(); /*061*/ }while(number[0] == 13); /*062*/ /*063*/ return; /*064*/ } /*065*/ SQ Lab. 37

/*066*/ void output. Function(FILE* io. File, int *point) /*067*/ { /*068*/ char number[11]; /*069*/

/*066*/ void output. Function(FILE* io. File, int *point) /*067*/ { /*068*/ char number[11]; /*069*/ char name[21]; /*070*/ char hp[14]; /*071*/ char output. Buffer[45]; /*072*/ /*073*/ do /*074*/ { /*075*/ system("cls"); /*076*/ puts("n 수록시킬 자료가 없으면 number에 0을 타이프 하시오"); /*077*/ goto. XY(1, 3); puts("number : "); /*078*/ goto. XY(25, 3); puts("name : "); /*079*/ goto. XY(27, 5); puts("hp : "); /*080*/ /*081*/ goto. XY(10, 3); gets(number); /*082*/ if(!strcmp(number, "0")) break; /*083*/ goto. XY(32, 3); gets(name); /*084*/ goto. XY(32, 5); gets(hp); /*085*/ /*086*/ sprintf(output. Buffer, "%10 s%-20 s%-13 sn", number, name, hp); /*087*/ fseek(io. File, *point, SEEK_SET); /*088*/ fprintf(io. File, "%s", output. Buffer); /*089*/ /*090*/ *point = ftell(io. File); /*091*/ /*092*/ }while(1); /*093*/ /*094*/ return; /*095*/ } SQ Lab. 38

/*096*/ /*097*/ void main() /*098*/ { /*099*/ char choice; /*100*/ char io. File. Name[30];

/*096*/ /*097*/ void main() /*098*/ { /*099*/ char choice; /*100*/ char io. File. Name[30]; /*101*/ int point = 0; /*102*/ /*103*/ FILE *io. File; /*104*/ printf("n Type "r+" mode file name = "); /*105*/ gets(io. File. Name); /*106*/ io. File = fopen(io. File. Name, "r+"); /*107*/ if(io. File == NULL) /*108*/ { /*109*/ printf("n Error: "%s" %s", io. File. Name, strerror(errno)); /*110*/ getche(); /*111*/ return; /*112*/ } /*113*/ SQ Lab. 39

/*114*/ do /*115*/ { /*116*/ system("cls"); /*117*/ printf("n 읽기='r' 쓰기='w' 끝내기='q' : "); /*118*/

/*114*/ do /*115*/ { /*116*/ system("cls"); /*117*/ printf("n 읽기='r' 쓰기='w' 끝내기='q' : "); /*118*/ choice = getche(); /*119*/ if(choice == 'q') break; /*120*/ switch(choice) /*121*/ { /*122*/ case 'r' : input. Function(io. File, &point); /*123*/ break; /*124*/ case 'w' : output. Function(io. File, &point); /*125*/ } /*126*/ }while(1); /*127*/ /*128*/ fclose(io. File); /*129*/ /*130*/ printf("nnn 끝났습니다(^@^), <Enter>하십시요. "); /*131*/ getchar(); /*132*/ } SQ Lab. 40