File Structure Chapter 4 Fundamental File Structure Concepts

  • Slides: 19
Download presentation
File Structure Chapter 4. Fundamental File Structure Concepts 전북대 전자정보공학부 장 재 우 교수

File Structure Chapter 4. Fundamental File Structure Concepts 전북대 전자정보공학부 장 재 우 교수

1. 필드와 레코드 구성 q (1) 스트림 화일(Stream Files) l 정보를 바이트(byte)의 스트림으로 표기

1. 필드와 레코드 구성 q (1) 스트림 화일(Stream Files) l 정보를 바이트(byte)의 스트림으로 표기 입력 출력 File Structures Mary Ames Alan Mason 123 Maple 90 Eastgate Stillwater, OK 74075 Ada, OK 74820 • A field is the smallest logically meaningful unit of information in file • field contains a single data value Ames. Mary 123 Maple. Stillwater. Ok 74075 Mason. Alan 90 Eastgate. Ada. OK 74820 - Chapter 4 - -2 - E-mail : jwchang@chonbuk. ac. kr

1. 필드와 레코드 구성 q Definition of record to hold person information In C++:

1. 필드와 레코드 구성 q Definition of record to hold person information In C++: In C: class Person { public: char last[11]; char first[11]; char address[16]; char city[16]; char state[3]; char zip[10]; }; struct Person { char last[11]; char first[11]; char address[16]; char city[16]; char state[3]; char zip[10]; }; File Structures - Chapter 4 - -3 - E-mail : jwchang@chonbuk. ac. kr

1. 필드와 레코드 구성 q (2) 필드(field) 구조 어떻게 필드를 유지할 것인가? l 방법

1. 필드와 레코드 구성 q (2) 필드(field) 구조 어떻게 필드를 유지할 것인가? l 방법 1 : 고정 길이 필드 ë 필요한 최대 크기 배정 Ames Mary Mason Alan 123 Maple Stillwater 90 Eastgate Ada OK 74075 OK 74820 ë 화일 크기가 커짐 ë 길이가 다양한 필드를 포함하는 데이터에는 부적합 ë 필드길이가 비슷한 경우 유용 l 방법 2 : 길이 지시자(length indicator) ë 필드의 앞에 필드의 길이를 저장 ë 길이기반(length-based) 필드 04 Ames 04 Mary 09123 Maple 10 Stillwater 02 OK 0574075 05 Mason 04 Alan 1190 Eastgate 03 Ada 02 OK 0574820 File Structures - Chapter 4 - -4 - E-mail : jwchang@chonbuk. ac. kr

1. 필드와 레코드 구성 q 필드(field) 구조 l 방법 3 : 구분자(delimiter)Ames|Mary|123 Maple|Stillwater|OK|74075 ë

1. 필드와 레코드 구성 q 필드(field) 구조 l 방법 3 : 구분자(delimiter)Ames|Mary|123 Maple|Stillwater|OK|74075 ë 구분문자로 필드 식별 Mason|Alan|90 Eastgate|Ada|OK|74820 ë 구분자 : 필드 내에 포함되지 않는 특수문자 l 방법 4 : 키워드(key word) ë 자체 기술적(self-describing)구조 : 화일 구성에 유용 ë 구분자와 결합하여 사용 ë 공간의 낭비 last=Ames|first=Mary|address=123 Maple|city=Stillwater| state=OK|zip=74075 File Structures - Chapter 4 - -5 - E-mail : jwchang@chonbuk. ac. kr

Field and Record Organization q (3) 필드(field)의 스트림 판독 istream & operator >> (istream

Field and Record Organization q (3) 필드(field)의 스트림 판독 istream & operator >> (istream & stream, Person & p) { // 화일로부터 구분자‘|’로 구분된 필드를 판독 char delim; stream. getline(p. Last. Name, 30, ‘|’); if( strlen(p. Last. Name) == 0 ) return stream; stream. getline(p. First. Name, 30, ‘|’); stream. getline(p. Address, 30, ‘|’); stream. getline(p. City, 30, ‘|’); stream. getline(p. State, 15, ‘|’); stream. getline(p. Zip. Code, 10, ‘|’); return stream; } < Person 객체에 구분된 필드를 판독하기 위한 추출 연산자 > File Structures - Chapter 4 - -6 - E-mail : jwchang@chonbuk. ac. kr

1. 필드와 레코드 구성 q (4) 레코드(record) 구조 l 방법 1 : 고정길이 레코드(fixed-length

1. 필드와 레코드 구성 q (4) 레코드(record) 구조 l 방법 1 : 고정길이 레코드(fixed-length record file) ë 각 레코드가 같은 수의 바이트로 구성 ë 필드 크기나 수를 고정하는 것은 아님 ë 가변길이 필드를 위한 컨테이너로 사용 Ames Mary Mason Alan 123 Maple Stillwater 90 Eastgate Ada OK 74075 OK 74820 unused space Ames|Mary|123 Maple|Stillwater|OK|74075 Mason|Alan|90 Eastgate|Ada|OK|74820 File Structures - Chapter 4 - -7 - E-mail : jwchang@chonbuk. ac. kr

1. 필드와 레코드 구성 q 레코드(record) 구조 l 방법 2 : 고정갯수 필드 ë

1. 필드와 레코드 구성 q 레코드(record) 구조 l 방법 2 : 고정갯수 필드 ë 각 레코드가 같은 수의 필드로 구성 ë 6개의 필드가 하나의 레코드 형성 Ames|Mary|123 Maple|Stillwater|OK|74075|Mason|Alan|90 Eastgate|Ada|OK|74820 l 방법 3 : 길이 지시자(length indicator) ë 각 레코드는 길이 지시자를 지님 ë 가변길이 레코드 처리에 널리 쓰임 40 Ames|Mary|123 Maple|Stillwater|OK|74075|36 Mason|Alan|90 Eastgate|Ada|OK|74820 File Structures - Chapter 4 - -8 - E-mail : jwchang@chonbuk. ac. kr

1. 필드와 레코드 구성 q 레코드(record) 구조 l 방법 4 : 주소 index 사용

1. 필드와 레코드 구성 q 레코드(record) 구조 l 방법 4 : 주소 index 사용 ë 레코드의 시작 주소 (바이트 오프셋: offset) 를 인덱스를 사용 Data file : Ames|Mary|123 Maple|Stillwater|OK|74075|Mason|Alan|90 Eastgate|Ada|OK|74820 Index file : 00 40 ………. . l 방법 5 : 구분자(delimiter) 사용 ë 각 레코드의 끝에 구분자를 놓음 ë ‘#’ 를 구분자로 사용 Ames|Mary|123 Maple|Stillwater|OK|74075|#Mason|Alan|90 Eastgate|Ada|OK|74820 File Structures - Chapter 4 - -9 - E-mail : jwchang@chonbuk. ac. kr

Field and Record Organization q 화일에 가변길이 레코드 기록하기 : 레코드 구조 방법(3) const

Field and Record Organization q 화일에 가변길이 레코드 기록하기 : 레코드 구조 방법(3) const int Max. Buffe. Size = 200; int Write. Person (ostream & stream, Person &p) { // 버퍼 생성 char buffer [Max. Buffer. Size]; // 각각의 필드에 구분자 ’|’를 덧붙여서 버퍼에 쓰기 strcpy(buffer, p. Last. Name); strcat(buffer, “|”); strcat(buffer, p. First. Name); strcat(buffer, “|”); strcat(buffer, p. Address); strcat(buffer, “|”); strcat(buffer, p. City); strcat(buffer, “|”); strcat(buffer, p. State); strcat(buffer, “|”); strcat(buffer, p. Zip. Code); strcat(buffer, “|”); // 버퍼에 기록된 레코드 길이를 구하여 화일에 써 넣기 short length = strlen(buffer); stream. write (&length, sizeof(length)); // 버퍼에 기록된 레코드 내용을 화일에 써 넣기 } stream. write (&buffer, length); < 가변길이 레코드(Person 객체)를 화일에 쓰기 > File Structures - Chapter 4 - - 11 - E-mail : jwchang@chonbuk. ac. kr

Field and Record Organization q 화일로부터 다양한 길이 레코드의 판독 : 레코드 구조 방법(3)

Field and Record Organization q 화일로부터 다양한 길이 레코드의 판독 : 레코드 구조 방법(3) ë 디스크에 있는 화일의 레코드 판독 ë 버퍼에 레코드를 저장 ë 레코드를 필드로 분리 Person Ames Mary 123 Maple Stillwater OK 74075 Buffer (length+1) 버퍼에서 읽기 Ames|Mary|123 Maple|Stillwater|OK|74075| 버퍼 생성 Get Record Length 필드 구분자 ‘|’ 제거 레코드를 버퍼에 읽어 오기 레코드 길이 읽기 File Structures - Chapter 4 - - 13 - E-mail : jwchang@chonbuk. ac. kr

Field and Record Organization q 화일로부터 다양한 길이 레코드의 판독 : 레코드 구조 방법(3)

Field and Record Organization q 화일로부터 다양한 길이 레코드의 판독 : 레코드 구조 방법(3) int Read. Variable. Person (istream & stream, Person & p) { // 화일에서 레코드 길이 읽기 & 버퍼 생성 short length; stream. read (&length, sizeof(length)); char *buffer = new char[length+1]; // 버퍼에 레코드 읽어 오기 stream. read (buffer, length); // 버퍼 내용을 문자열 타입으로 변환하여 operator>>() 함수를 사용할 수 있도록 함 buffer [length] = 0; // terminate buffer with Null istrstream strbuff (buffer); // operator>>() 함수를 호출하여, // 버퍼로부터 필드 구분자 ‘|’ 를 제거하고 각 필드를 Person 객체에 읽어 오기 strbuff >> p; return 1; } < 화일에서 가변길이 레코드(Person 객체) 읽기 > File Structures - Chapter 4 - - 14 - E-mail : jwchang@chonbuk. ac. kr

Field and Record Organization q (6) 문자와 수의 혼합 : 화일 덤프 사용 Offset

Field and Record Organization q (6) 문자와 수의 혼합 : 화일 덤프 사용 Offset 000000020 0000040 0000060 0000100 File Structures Values ( 0028 M a 4 d 61 | O 7 c 4 f n | 6 e 7 c t e 7465 A m e s | M a r y | 1 2 3 416 d 6573 7 c 4 d 6172 797 c 3132 3320 p l e | S t i l l w a t e r 706 c 657 c 5374 696 c 6 c 77 6174 6572 K | 7 4 0 7 5 | $ M a s o 4 b 7 c 3734 3037 357 c 0024 4 d 61 736 f A l a n | 9 0 E a s t g a 416 c 616 e 7 c 39 3020 4561 7374 6761 | A d a | O K | 7 4 8 2 0 | 7 c 41 6461 7 c 4 f 4 b 7 c 3734 3832 307 c - Chapter 4 - - 15 - E-mail : jwchang@chonbuk. ac. kr

3. Using Inheritance for Record Buffer Classes q 기초 클래스 : IOBuffer l 각

3. Using Inheritance for Record Buffer Classes q 기초 클래스 : IOBuffer l 각 메소드들은 virtual로 선언되어 자신의 구현을 서브클래스에서 정의할 수 있도록 함 q Variable. Length. Buffer l Delimited. Field. Buffer l Length. Field. Buffer q Variable. Length. Buffer l Fixed. Field. Buffer q 부록 F 에 전체 클래스 및 메소드가 수록 File Structures - Chapter 4 - - 17 - E-mail : jwchang@chonbuk. ac. kr

3. Using Inheritance for Record Buffer Classes q 레코드 버퍼 객체를 위한 클래스 계층

3. Using Inheritance for Record Buffer Classes q 레코드 버퍼 객체를 위한 클래스 계층 IOBuffer 버퍼값에 대한 문자 배열 Variable. Length. Buffer 가변길이 레코드에 대한 판독/기록 연산 Delimited. Field. Buffer 구분된 필드에 대한 팩/언팩 연산 File Structures Fixed. Length. Buffer 고정길이 레코드에 대한 판독/기록 연산 Length. Field. Buffer 길이기반 필드에 대한 팩/언팩 연산 - Chapter 4 - Fixed. Field. Buffer 고정길이 필드에 대한 팩/언팩 연산 - 18 - E-mail : jwchang@chonbuk. ac. kr