TAILIEUCHUNG - Lecture Introduction to Computer Programming - Lecture 29

The contents of this chapter include all of the following: Collections provided in namespace, collections store object reference, traversal supported, ArrayList can be sorted. | CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 29 Thanks for Lecture Slides: 1 File Processing (Continued) 2 Random-Access Files Random access files Access individual records without searching through other records Instant access to records in a file Data can be inserted without destroying other data Data previously stored can be updated or deleted without overwriting Implemented using fixed length records Sequential files do not have fixed length records 0 200 300 400 500 byte offsets } } } } } } } 100 100 bytes 100 bytes 100 bytes 100 bytes 100 bytes 100 bytes 3 Creating a Randomly Accessed File Data in random access files Unformatted (stored as "raw bytes") All data of the same type (int, for example) uses the same amount of memory All records of the same type have a fixed length Data not human readable 4 Creating a Randomly Accessed File Unformatted I/O functions fwrite Transfer bytes from a location in memory to a file fread Transfer bytes from a file to a location in memory Example: fwrite( &number, sizeof( int ), 1, myPtr ); &number – Location to transfer bytes from sizeof( int ) – Number of bytes to transfer 1 – For arrays, number of elements to transfer In this case, "one element" of an array is being transferred myPtr – File to transfer to or from 5 Creating a Randomly Accessed File Writing structs fwrite( &myObject, sizeof (struct myStruct), 1, myPtr ); sizeof – returns size in bytes of object in parentheses To write several array elements Pointer to array as first argument Number of elements to write as third argument 6 /* Fig. : #include /* clientData structure definition */ struct clientData { int acctNum; /* account number */ char lastName[ 15 ]; /* account last name */ char firstName[ 10 ]; /* account first name */ double balance; /* account balance */ }; /* end structure clientData */ int main( void ) { int i;

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.