Presentation is loading. Please wait.

Presentation is loading. Please wait.

조 병 규 Software Quality Lab. 한국교통대학교

Similar presentations


Presentation on theme: "조 병 규 Software Quality Lab. 한국교통대학교"— Presentation transcript:

1 조 병 규 Software Quality Lab. 한국교통대학교
문제해결기법 12 파일의 입/출력 포인터 처리 조 병 규 Software Quality Lab. 한국교통대학교 SQ Lab.

2 ftell() 함수 long ftell(FILE *Lfilename); 현재의 파일 포인터 위치를 취득한다. ■리턴값
∙ 성공 : 포인터 위치 ∙ 실패 : -1L ■파라미터 설명 ∙ Lfilename : 논리파일 이름 현재의 파일 포인터 위치를 취득한다. SQ Lab.

3 예제 : ftell() 입력위치 알아보기 /*001*/ /* ftell01r00 */ /*002*/
/*003*/ #include "stdio.h" /*004*/ #include "stdlib.h" /*005*/ #include "string.h" /*006*/ /*007*/ #pragma warning(push, 2) /*008*/ SQ Lab.

4 /*014*/ char inputFileName[30]; /*015*/ /*016*/ FILE *inputFile;
/*009*/ void main() /*010*/ { /*011*/ char number[11]; /*012*/ char name[21]; /*013*/ char hp[14]; /*014*/ char inputFileName[30]; /*015*/ /*016*/ FILE *inputFile; /*017*/ printf("\n Type input file name = "); /*018*/ gets(inputFileName); /*019*/ inputFile = fopen(inputFileName, "r"); /*020*/ if(inputFile == NULL) /*021*/ { /*022*/ printf("\n Error: \"%s\" %s", inputFileName, strerror(errno)); /*023*/ getchar(); /*024*/ return; /*025*/ } /*026*/ SQ Lab.

5 /*029*/ position = ftell(inputFile);
/*027*/ int position; /*028*/ /*029*/ position = ftell(inputFile); /*030*/ fread(number, 10, 1, inputFile); /*031*/ number[10] = '\0'; /*032*/ printf("\n (%03i)%s", position, number); /*033*/ /*034*/ position = ftell(inputFile); /*035*/ fread(name, 20, 1, inputFile); /*036*/ name[20] = '\0'; /*037*/ printf("\n (%03i)%s", position, name); /*038*/ /*039*/ position = ftell(inputFile); /*040*/ fread(hp, 14, 1, inputFile); /*041*/ hp[13] = '\0'; /*042*/ printf("\n (%03i)%s", position, hp); /*043*/ /*044*/ position = ftell(inputFile); /*045*/ printf("\n (%03i)", position); /*046*/ /*047*/ fclose(inputFile); /*048*/ SQ Lab.

6 /*049*/ printf("\n\n\n 아시겠지요(^@^)... <Enter>하십시요. ");
/*050*/ getchar(); /*051*/ } SQ Lab.

7 ■입력 파일 ■화면 출력 SQ Lab.

8 예제 : ftell() 출력위치 알아보기 /*001*/ /* ftell01w00 */ /*002*/
/*003*/ #include "stdio.h" /*004*/ #include "windows.h" /*005*/ /*006*/ #pragma warning(push, 2) /*007*/ SQ Lab.

9 /*014*/ FILE *outputFile;
/*008*/ void main() /*009*/ { /*010*/ char number[11] = "12345"; /*011*/ char name[21] = "a "; /*012*/ char hp[14] = " "; /*013*/ /*014*/ FILE *outputFile; /*015*/ outputFile = fopen("c:\\files\\ftell01w00.txt", "w"); /*016*/ if(outputFile == NULL) /*017*/ { /*018*/ printf("\n *** \"c:\\files\\ftell01w00.txt\" not opened ***"); /*019*/ getchar(); /*020*/ exit(0); /*021*/ } /*022*/ SQ Lab.

10 /*025*/ position = ftell(outputFile);
/*023*/ int position; /*024*/ /*025*/ position = ftell(outputFile); /*026*/ printf("\n position(%02i)%s", position, number); /*027*/ fwrite(number, 10, 1, outputFile); /*028*/ /*029*/ position = ftell(outputFile); /*030*/ printf("\n position(%02i)%s", position, name); /*031*/ fwrite(name, 20, 1, outputFile); /*032*/ /*033*/ printf("\n position(%02i)%s", ftell(outputFile), hp); /*034*/ fwrite(hp, 13, 1, outputFile); /*035*/ /*036*/ printf("\n position(%02i)%s", ftell(outputFile), "\"\\n\""); /*037*/ fwrite("\n", 1, 1, outputFile); /*038*/ /*039*/ printf("\n position(%02i)", ftell(outputFile)); /*040*/ /*041*/ fclose(outputFile); /*042*/ /*043*/ printf("\n\n 파일이 만들어 졌습니다.<Enter>하십시요."); /*044*/ getchar(); /*045*/ } SQ Lab.

11 ■화면 출력 ■생성 파일 SQ Lab.

12 int fseek(FILE *Lfilename, long offset, int origin);
■리턴값 ∙ 성공 : 0 ∙ 실패 : 0 이외의 값 ■파라미터 설명 ∙ Lfilename : 논리파일 이름 ∙ offset : origin을 기준으로 한 이동 거리 ∙ origin : 포인터 기준 파일 포인터 위치를 새롭게 설정시킨다. SQ Lab.

13 origin 포인터 기준 의미 SEEK_SET (0) SEEK_CUR (1) SEEK_END (2) 파일의 시작 점
포인터의 현재 위치 파일의 끝 지점 SQ Lab.

14 예제 : fseek() 입력의 경우 /*001*/ /* fseek01r00 */ /*002*/
/*003*/ #include "stdio.h" /*004*/ #include "stdlib.h" /*005*/ #include "string.h" /*006*/ /*007*/ #pragma warning(push, 2) /*008*/ SQ Lab.

15 /*014*/ char inputFileName[30]; /*015*/ /*016*/ FILE *inputFile;
/*009*/ void main() /*010*/ { /*011*/ char number[11]; /*012*/ char name[21]; /*013*/ char hp[14]; /*014*/ char inputFileName[30]; /*015*/ /*016*/ FILE *inputFile; /*017*/ printf("\n Type input file name = "); /*018*/ gets(inputFileName); /*019*/ inputFile = fopen(inputFileName, "r"); /*020*/ if(inputFile == NULL) /*021*/ { /*022*/ printf("\n Error: \"%s\" %s", inputFileName, strerror(errno)); /*023*/ getchar(); /*024*/ return; /*025*/ } /*026*/ SQ Lab.

16 /*029*/ fseek(inputFile, 10, SEEK_SET);
/*027*/ int position; /*028*/ /*029*/ fseek(inputFile, 10, SEEK_SET); /*030*/ position = ftell(inputFile); /*031*/ printf("\n\n position(%03d)", position); /*032*/ fread(name, 20, 1, inputFile); /*033*/ name[20] = '\0'; /*034*/ printf(":%s", name); /*035*/ /*036*/ fseek(inputFile, -30, SEEK_CUR); /*037*/ position = ftell(inputFile); /*038*/ printf("\n\n position(%03d)", position); /*039*/ fread(number, 10, 1, inputFile); /*040*/ number[10] = '\0'; /*041*/ printf(":%s", number); /*042*/ /*043*/ fseek(inputFile, -15, SEEK_END); /*044*/ position = ftell(inputFile); /*045*/ printf("\n\n position(%03d)", position); /*046*/ fread(hp, 14, 1, inputFile); /*047*/ hp[13] = '\0'; /*048*/ printf(":%s", hp); SQ Lab.

17 /*050*/ fclose(inputFile); /*051*/
/*049*/ /*050*/ fclose(inputFile); /*051*/ /*052*/ printf("\n\n\n <Enter>하십시요. "); /*053*/ getchar(); /*054*/ } SQ Lab.

18 /*014*/ char inputFileName[30];
/*017*/ printf("\n Type input file name = "); /*018*/ gets(inputFileName); SQ Lab.

19 /*029*/ fseek(inputFile, 10, SEEK_SET);
SQ Lab.

20 /*036*/ fseek(inputFile, -30, SEEK_CUR);
SQ Lab.

21 /*043*/ fseek(inputFile, -15, SEEK_END);
나타나지 않으나 뒤에 CR-LF가 수록되어 있다. SQ Lab.

22 ■화면 결과 SQ Lab.

23 예제 : fseek() 출력의 경우 /*001*/ /* fseek01w00 */ /*002*/
/*003*/ #include "stdio.h" /*004*/ #include "windows.h" /*005*/ /*006*/ #pragma warning(push, 2) /*007*/ SQ Lab.

24 /*013*/ FILE *outputFile; /*014*/
/*008*/ void main() /*009*/ { /*010*/ char number[11] = "12345"; /*011*/ char name[21] = "a "; /*012*/ char hp[14] = " "; /*013*/ FILE *outputFile; /*014*/ /*015*/ outputFile = fopen("c:\\files\\fseek01w00.txt", "w"); /*016*/ if(outputFile == NULL) /*017*/ { /*018*/ printf("\n *** \"c:\\files\\fseek01w00.txt\" not opened ***"); /*019*/ getchar(); /*020*/ exit(0); /*021*/ } /*022*/ SQ Lab.

25 /*023*/ fseek(outputFile, 10, SEEK_SET);
/*024*/ fwrite(name, 20, 1, outputFile); /*025*/ /*026*/ fseek(outputFile, -30, SEEK_CUR); /*027*/ fwrite(number, 10, 1, outputFile); /*028*/ /*029*/ fseek(outputFile, 30, SEEK_SET); /*030*/ fwrite(hp, 13, 1, outputFile); /*031*/ fwrite("\n", 1, 1, outputFile); /*032*/ /*033*/ fclose(outputFile); /*034*/ /*035*/ printf("\n\n 파일이 만들어 졌습니다.<Enter>하십시요."); /*036*/ getchar(); /*037*/ } SQ Lab.

26 ■출력 파일 SQ Lab.

27 fgetpos() 함수 int fgetpos(FILE *Lfilename, fpos_t *posistion); ■리턴값
∙ 성공 : 0 ∙ 실패 : 0이 아닌 값 ■파라미터 설명 ∙ Lfilename : 논리파일 이름 ∙ posistion : 위치를 수록할 장소(fpos_t = long integer) 파일 위치 지시자의 값을 취득한다. ftell()과 비슷한 기능이다. SQ Lab.

28 예제 : fgetpos() 입력의 경우 /*001*/ /* fgetpos01r00 */ /*002*/
/*003*/ #include "stdio.h" /*004*/ #include "stdlib.h" /*005*/ #include "string.h" /*006*/ /*007*/ #pragma warning(push, 2) /*008*/ SQ Lab.

29 /*011*/ char inputFileName[30]; /*012*/ /*013*/ FILE *inputFile;
/*009*/ void main() /*010*/ { /*011*/ char inputFileName[30]; /*012*/ /*013*/ FILE *inputFile; /*014*/ printf("\n Type input file name = "); /*015*/ gets(inputFileName); /*016*/ inputFile = fopen(inputFileName, "r"); /*017*/ if(inputFile == NULL) /*018*/ { /*019*/ printf("\n Error: \"%s\" %s", inputFileName, strerror(errno)); /*020*/ getchar(); /*021*/ return; /*022*/ } /*023*/ SQ Lab.

30 /*031*/ returnValue = fgetpos(inputFile, &position);
/*024*/ char number[11]; /*025*/ char name[21]; /*026*/ char hp[14]; /*027*/ /*028*/ int returnValue; /*029*/ fpos_t position; /*030*/ /*031*/ returnValue = fgetpos(inputFile, &position); /*032*/ if(returnValue == 0) /*033*/ { /*034*/ fread(number, 10, 1, inputFile); /*035*/ number[10] = '\0'; /*036*/ printf("\n (%03ld)", position); /*037*/ printf("%s", number); /*038*/ } /*039*/ else /*040*/ { /*041*/ printf("\n\n fgetpos() error : (%i) ", returnValue); /*042*/ exit(0); /*043*/ } /*044*/ SQ Lab.

31 /*045*/ fgetpos(inputFile, &position);
/*046*/ fread(name, 20, 1, inputFile); /*047*/ name[20] = '\0'; /*048*/ printf("\n (%03ld)", position); /*049*/ printf("%s", name); /*050*/ /*051*/ fgetpos(inputFile, &position); /*052*/ fread(hp, 14, 1, inputFile); /*053*/ hp[13] = '\0'; /*054*/ printf("\n (%03ld)", position); /*055*/ printf("%s", hp); /*056*/ /*057*/ fgetpos(inputFile, &position); /*058*/ printf("\n (%03ld)", position); /*059*/ /*060*/ fclose(inputFile); /*061*/ /*062*/ printf("\n\n\n <Enter>하십시요. "); /*063*/ getchar(); /*064*/ } SQ Lab.

32 ■입력 파일 ■화면 출력 SQ Lab.

33 예제 : fgetpos() 출력의 경우 /*001*/ /* fgetpos01w00 */ /*002*/
/*003*/ #include "stdio.h" /*004*/ #include "windows.h" /*005*/ /*006*/ #pragma warning(push, 2) /*007*/ SQ Lab.

34 /*014*/ FILE *outputFile;
/*008*/ void main() /*009*/ { /*010*/ char number[11] = "12345"; /*011*/ char name[21] = "a "; /*012*/ char hp[14] = " "; /*013*/ /*014*/ FILE *outputFile; /*015*/ outputFile = fopen("c:\\files\\fgetpos01w00.txt", "w"); /*016*/ if(outputFile == NULL) /*017*/ { /*018*/ printf("\n \"c:\\files\\fgetpos01w00.txt\" not opened"); /*019*/ getchar(); /*020*/ exit(0); /*021*/ } /*022*/ SQ Lab.

35 /*026*/ returnValue = fgetpos(outputFile, &position);
/*023*/ fpos_t position; /*024*/ int returnValue; /*025*/ /*026*/ returnValue = fgetpos(outputFile, &position); /*027*/ if(returnValue == 0) /*028*/ { /*029*/ printf("\n (%03ld)", position); /*030*/ printf("%s", number); /*031*/ fwrite(number, 10, 1, outputFile); /*032*/ } /*033*/ else /*034*/ { /*035*/ printf("\n\n fgetpos() error : (%i) ", returnValue); /*036*/ exit(0); /*037*/ } SQ Lab.

36 /*038*/ fgetpos(outputFile, &position);
/*039*/ printf("\n (%03ld)", position); /*040*/ printf("%s", name); /*041*/ fwrite(name, 20, 1, outputFile); /*042*/ fgetpos(outputFile, &position); /*043*/ printf("\n (%03ld)", position); /*044*/ printf("%s", hp); /*045*/ fwrite(hp, 13, 1, outputFile); /*046*/ fgetpos(outputFile, &position); /*047*/ printf("\n (%03ld)", position); /*048*/ printf("%s", "new line code"); /*049*/ fwrite("\n", 1, 1, outputFile); /*050*/ fgetpos(outputFile, &position); /*051*/ printf("\n (%03ld)", position); /*052*/ /*053*/ fclose(outputFile); /*054*/ /*055*/ printf("\n\n 파일이 만들어 졌습니다.<Enter>하십시요."); /*056*/ getchar(); /*057*/ } SQ Lab.

37 ■화면 출력 ■생성 파일 SQ Lab.

38 fsetpos() 함수 int fsetpos(FILE *Lfilename, const fpos_t *position);
■리턴값 ∙ 성공 : 0 ∙ 실패 : 0 이외의 값 ■파라미터 설명 ∙ Lfilename : 논리파일 이름 ∙ position : 설정 위치 파일 지시자를 새롭게 설정한다. fseek()와 비슷한 기능이다. SQ Lab.

39 예제 : fsetpos() 입력의 경우 /*001*/ /* fsetpos01r00 */ /*002*/
/*003*/ #include "stdio.h" /*004*/ #include "stdlib.h" /*005*/ #include "string.h" /*006*/ /*007*/ #pragma warning(push, 2) /*008*/ SQ Lab.

40 /*011*/ char inputFileName[30]; /*012*/ /*013*/ FILE *inputFile;
/*009*/ void main() /*010*/ { /*011*/ char inputFileName[30]; /*012*/ /*013*/ FILE *inputFile; /*014*/ printf("\n Type input file name = "); /*015*/ gets(inputFileName); /*016*/ inputFile = fopen(inputFileName, "r"); /*017*/ if(inputFile == NULL) /*018*/ { /*019*/ printf("\n Error: \"%s\" %s", inputFileName, strerror(errno)); /*020*/ getchar(); /*021*/ return; /*022*/ } /*023*/ SQ Lab.

41 /*031*/ returnValue = fsetpos(inputFile, &position);
/*024*/ char number[11]; /*025*/ char name[21]; /*026*/ char hp[14]; /*027*/ fpos_t position; /*028*/ int returnValue; /*029*/ /*030*/ position = 10; /*031*/ returnValue = fsetpos(inputFile, &position); /*032*/ if(returnValue == 0) /*033*/ { /*034*/ printf("\n (%03d)", position); /*035*/ fread(name, 20, 1, inputFile); /*036*/ name[20] = '\0'; /*037*/ printf("%s", name); /*038*/ } /*039*/ else /*040*/ { /*041*/ printf("\n\n fsetpos() error : (%i) ", returnValue); /*042*/ exit(0); /*043*/ } /*044*/ SQ Lab.

42 /*046*/ fsetpos(inputFile, &position);
/*047*/ printf("\n (%03d)", position); /*048*/ fread(number, 10, 1, inputFile); /*049*/ number[10] = '\0'; /*050*/ printf("%s", number); /*051*/ fgetpos(inputFile, &position); /*052*/ /*053*/ position = position + 20; /*054*/ fsetpos(inputFile, &position); /*055*/ printf("\n (%03d)", position); /*056*/ fread(hp, 14, 1, inputFile); /*057*/ hp[13] = '\0'; /*058*/ printf("%s", hp); /*059*/ /*060*/ fclose(inputFile); /*061*/ /*062*/ printf("\n\n\n <Enter>하십시요. "); /*063*/ getchar(); /*064*/ } SQ Lab.

43 ■입력 파일 ■화면 출력 SQ Lab.

44 예제 : fsetpos() 출력의 경우 /*001*/ /* fsetpos01w00 */ /*002*/
/*003*/ #include "stdio.h" /*004*/ #include "windows.h" /*005*/ /*006*/ #pragma warning(push, 2) /*007*/ " SQ Lab.

45 /*014*/ FILE *outputFile;
/*008*/ void main() /*009*/ { /*010*/ char number[11] = "12345"; /*011*/ char name[21] = "a "; /*012*/ char hp[14] = " "; /*013*/ /*014*/ FILE *outputFile; /*015*/ outputFile = fopen("c:\\files\\fsetpos01w00.txt", "w"); /*016*/ if(outputFile == NULL) /*017*/ { /*018*/ printf("\n \"c:\\files\\fsetpos01w00.txt\" not opened"); /*019*/ getchar(); /*020*/ exit(0); /*021*/ } /*022*/ SQ Lab.

46 /*027*/ returnValue = fsetpos(outputFile, &position);
/*023*/ fpos_t position; /*024*/ int returnValue; /*025*/ position = 10; /*026*/ /*027*/ returnValue = fsetpos(outputFile, &position); /*028*/ if(returnValue == 0) /*029*/ { /*030*/ fwrite(name, 20, 1, outputFile); /*031*/ printf("\n (%03d)", position); /*032*/ printf("%s", name); /*033*/ } /*034*/ else /*035*/ { /*036*/ printf("\n\n fsetpos() error : (%i) ", returnValue); /*037*/ exit(0); /*038*/ } /*039*/ SQ Lab.

47 /*041*/ fsetpos(outputFile, &position);
/*042*/ fwrite(number, 10, 1, outputFile); /*043*/ printf("\n (%03d)", position); /*044*/ printf("%s", number); /*045*/ fgetpos(outputFile, &position); /*046*/ /*047*/ position = position + 20; /*048*/ fsetpos(outputFile, &position); /*049*/ fwrite(hp, 13, 1, outputFile); /*050*/ printf("\n (%03d)", position); /*051*/ printf("%s", hp); /*052*/ /*053*/ fwrite("\n", 1, 1, outputFile); /*054*/ /*055*/ fclose(outputFile); /*056*/ /*057*/ printf("\n\n 파일이 만들어 졌습니다.<Enter>하십시요."); /*058*/ getchar(); /*059*/ } SQ Lab.

48 ■화면 출력 ■생성 파일 SQ Lab.

49 [실습1] ftell(), fgetpos 한 레코드씩 입력하며 파일 포인터 위치 알아보기
자판으로부터 물리파일 이름을 입력받고, 지정 파일의 자료를 한 레코드씩 입력하며 그때의 파일 포인터 위치를 알아보는 프로그램을 작성하시오. Program : ftell01r01 fgetpos01r01 SQ Lab.

50 ■입력 파일 : c:\files\x01.txt ■화면 출력 SQ Lab.

51 [실습1 program1] ftell01r01 /*001*/ /* ftell01r01 */ /*002*/
/*003*/ #include "stdio.h" /*004*/ #include "stdlib.h" /*005*/ #include "string.h" /*006*/ /*007*/ #pragma warning(push, 2) /*008*/ /*009*/ #define lineSize 44 /*010*/ SQ Lab.

52 /*014*/ char inputFileName[30]; /*015*/ char inputBuffer[lineSize];
/*011*/ void main() /*012*/ { /*013*/ int position; /*014*/ char inputFileName[30]; /*015*/ char inputBuffer[lineSize]; /*016*/ /*017*/ FILE *inputFile; /*018*/ printf("\n Type input file name = "); /*019*/ gets(inputFileName); /*020*/ inputFile = fopen(inputFileName, "r"); /*021*/ if(inputFile == NULL) /*022*/ { /*023*/ printf("\n Error: \"%s\" %s", inputFileName, strerror(errno)); /*024*/ getchar(); /*025*/ return; /*026*/ } /*027*/ SQ Lab.

53 /*030*/ position = ftell(inputFile);
/*028*/ do /*029*/ { /*030*/ position = ftell(inputFile); /*031*/ printf("\n (%03d)", position); /*032*/ fread(inputBuffer, lineSize, 1, inputFile); /*033*/ if(feof(inputFile)) break; /*034*/ inputBuffer[lineSize-1] = '\0'; /*035*/ printf("%s", inputBuffer); /*036*/ }while(1); /*037*/ /*038*/ fclose(inputFile); /*039*/ /*040*/ printf("\n\n\n <Enter>하십시요. "); /*041*/ getchar(); /*042*/ } SQ Lab.

54 [실습1 program2] fgetpos01r01 /*001*/ /* fgetpos01r01 */ /*002*/
/*003*/ #include "stdio.h" /*004*/ #include "stdlib.h" /*005*/ #include "string.h" /*006*/ /*007*/ #pragma warning(push, 2) /*008*/ SQ Lab.

55 /*011*/ char inputFileName[30]; /*012*/ /*013*/ FILE *inputFile;
/*009*/ void main() /*010*/ { /*011*/ char inputFileName[30]; /*012*/ /*013*/ FILE *inputFile; /*014*/ printf("\n Type input file name = "); /*015*/ gets(inputFileName); /*016*/ inputFile = fopen(inputFileName, "r"); /*017*/ if(inputFile == NULL) /*018*/ { /*019*/ printf("\n Error: \"%s\" %s", inputFileName, strerror(errno)); /*020*/ getchar(); /*021*/ return; /*022*/ } /*023*/ /*024*/ int returnValue; /*025*/ fpos_t position = 0; /*026*/ char inputBuffer[44]; /*027*/ SQ Lab.

56 /*030*/ returnValue = fgetpos(inputFile, &position);
/*028*/ do /*029*/ { /*030*/ returnValue = fgetpos(inputFile, &position); /*031*/ if(returnValue == 0) /*032*/ { /*033*/ fread(inputBuffer, 44, 1, inputFile); /*034*/ printf("\n (%03ld)", position); /*035*/ if(feof(inputFile)) break; /*036*/ inputBuffer[43] = '\0'; /*037*/ printf("%s", inputBuffer); /*038*/ } /*039*/ else /*040*/ { /*041*/ printf("\n\n fgetpos() error : (%i) ", returnValue); /*042*/ exit(0); /*043*/ } /*044*/ }while(1); /*045*/ SQ Lab.

57 /*046*/ fclose(inputFile); /*047*/
/*048*/ printf("\n\n\n <Enter>하십시요. "); /*049*/ getchar(); /*050*/ } SQ Lab.

58 [실습2] fseek(), fsetpos 한 레코드씩 건너 입력해 파일 포인터 위치 알아보기
자판으로부터 물리파일 이름을 입력받고, 지정 파일을 한 레코드씩 건너 입력하며 그때의 파일 포인터 위치를 알아보는 프로그램을 작성하시오. Program : fseek01r01 fsetpos01r01 SQ Lab.

59 ■입력 파일 : c:\files\x01.txt ■화면 출력 SQ Lab.

60 [실습2 program1] fseek01r01 /*001*/ /* fseek01r01 */ /*002*/
/*003*/ #include "stdio.h" /*004*/ #include "stdlib.h" /*005*/ #include "string.h" /*006*/ /*007*/ #pragma warning(push, 2) /*008*/ /*009*/ #define lineSize 44 /*010*/ SQ Lab.

61 /*014*/ char inputFileName[30]; /*015*/ char inputBuffer[lineSize];
/*011*/ void main() /*012*/ { /*013*/ int position; /*014*/ char inputFileName[30]; /*015*/ char inputBuffer[lineSize]; /*016*/ /*017*/ FILE *inputFile; /*018*/ printf("\n Type input file name = "); /*019*/ gets(inputFileName); /*020*/ inputFile = fopen(inputFileName, "r"); /*021*/ if(inputFile == NULL) /*022*/ { /*023*/ printf("\n Error: \"%s\" %s", inputFileName, strerror(errno)); /*024*/ getchar(); /*025*/ return; /*026*/ } /*027*/ SQ Lab.

62 /*030*/ position = ftell(inputFile) + 45;
/*028*/ do /*029*/ { /*030*/ position = ftell(inputFile) + 45; /*031*/ printf("\n (%03d)", position); /*032*/ fseek(inputFile, position, SEEK_SET); /*033*/ fread(inputBuffer, lineSize, 1, inputFile); /*034*/ if(feof(inputFile)) break; /*035*/ inputBuffer[lineSize-1] = '\0'; /*036*/ printf("%s", inputBuffer); /*037*/ }while(1); /*038*/ /*039*/ fclose(inputFile); /*040*/ /*041*/ printf("\n\n\n <Enter>하십시요. "); /*042*/ getchar(); /*043*/ } SQ Lab.

63 [실습2 program2] fsetpos01r01 /*001*/ /* fsetpos01r01 */ /*002*/
/*003*/ #include "stdio.h" /*004*/ #include "stdlib.h" /*005*/ #include "string.h" /*006*/ /*007*/ #pragma warning(push, 2) /*008*/ /*009*/ #define lineSize 44 /*010*/ SQ Lab.

64 /*013*/ char inputFileName[30]; /*014*/ /*015*/ FILE *inputFile;
/*011*/ void main() /*012*/ { /*013*/ char inputFileName[30]; /*014*/ /*015*/ FILE *inputFile; /*016*/ printf("\n Type input file name = "); /*017*/ gets(inputFileName); /*018*/ inputFile = fopen(inputFileName, "r"); /*019*/ if(inputFile == NULL) /*020*/ { /*021*/ printf("\n Error: \"%s\" %s", inputFileName, strerror(errno)); /*022*/ getchar(); /*023*/ return; /*024*/ } /*025*/ /*026*/ fpos_t position; /*027*/ int returnValue; /*028*/ char inputBuffer[lineSize]; /*029*/ SQ Lab.

65 /*032*/ fgetpos(inputFile, &position);
/*030*/ do /*031*/ { /*032*/ fgetpos(inputFile, &position); /*033*/ position = position + 45; /*034*/ returnValue = fsetpos(inputFile, &position); /*035*/ if(returnValue == 0) /*036*/ { /*037*/ printf("\n (%03d)", position); /*038*/ fread(inputBuffer, lineSize, 1, inputFile); /*039*/ if(feof(inputFile)) break; /*040*/ inputBuffer[lineSize-1] = '\0'; /*041*/ printf("%s", inputBuffer); /*042*/ } /*043*/ else /*044*/ { /*045*/ printf("\n\n fsetpos() error : (%i) ", returnValue); /*046*/ exit(0); /*047*/ } /*048*/ }while(1); SQ Lab.

66 /*050*/ fclose(inputFile); /*051*/
/*049*/ /*050*/ fclose(inputFile); /*051*/ /*052*/ printf("\n\n\n <Enter>하십시요. "); /*053*/ getchar(); /*054*/ } SQ Lab.


Download ppt "조 병 규 Software Quality Lab. 한국교통대학교"

Similar presentations


Ads by Google