120 likes | 332 Views
Tutorial 7. Binary files (Random Access files) in C fread/fwrite/fseek/etc. Command line arguments. Binary files. Binary files are very similar to arrays of records The records are in a disk file rather than in an array in memory
E N D
Tutorial 7 • Binary files (Random Access files) in C • fread/fwrite/fseek/etc. • Command line arguments
Binary files • Binary files are very similar to arrays of records • The records are in a disk file rather than in an array in memory • You can create very large collections of records (limited only by your available disk space) • They are also permanent and always available • A disadvantage (compared to an array) is the slowness that comes from disk access time
Binary files • Binary files have two features that distinguish them from text files: • You can jump instantly to any record in the file, which provides random access as in an array (the file actually mirrors the physical memory) • You can change the contents of a record anywhere in the file at any time
Main functions • fopen • fclose • fread • fwrite • fseek • ftell
fread/fwrite • Both fread & fwrite functions take four arguments: • A memory address • The number of bytes to read per block • The number of blocks to read • The file variable
fread/fwrite • Example: • fread(&myrec,sizeof(struct rec),1,f); specifies size of rec bytes to be read from the file pointed to by f from the current location of the file pointer. • The block read will be stored at the memory address &myrec. • The fwrite function works the same way, but moves the block of bytes from memory to the file.
fseek • The fseek function moves the file pointer to an arbitrary byte in the file. • You can use three options when seeking: • SEEK_SET - moves the file pointer down from the beginning of the file, i.e. from byte 0. • SEEK_CUR - moves the file pointer down from its current position. • SEEK_END - moves the file pointer up from the end of file. Note you must use negative offsets. • Example: fseek(f,sizeof(struct rec)*3,SEEK_SET);
Exercise 1 • Download, extract, and make tut10.tar • Run “./generate_file 10 myfile” • The file “myfile” now contains a list of 10 sorted numbers • Use vi or more to view its content • Now run “./print_file myfile” • How can you explain the different results?
Exercise 2 – file statistics • The two source codes provided demonstrate the use of fread/fwrite/fseek/ftell • Use them and the course slides if necessary in order to implement the following functions: • print_max_element – • prints the largest element in the file • print_average – • prints the average of the elements in the file • quick_search(int num) – • returns num's position (e.g. element #7) in the file, or -1 if it's not in the file
Exercise 2 – hints • print_max_element – the file is sorted! • print_average – have a look at print_file • quick_search(int num) – • A fast way to search for a key in a sorted array is to use a binary search. The idea is to look at the element in the middle. If the key is equal to that, the search is finished. If the key is less than the middle element, do a binary search on the first half. If it's greater, do a binary search of the second half.
BinarySearch(A[0..N-1], value) { low = 0 high = N - 1 while (low <= high) { mid = (low + high) / 2 if (A[mid] > value) high = mid - 1 else if (A[mid] < value) low = mid + 1 else return mid // found } return -1 // not found } Pseudo code for binary search Note: you should adapt this code in order to implement the search directly on the file; i.e., you should fseek to the required position and fread the element at that position.
Exercise 2 – cont'd • For you convenience a file skeleton statistics.c is provided • Start by implementing print_max_element, then print_average, and eventually quick_search • You can use ./generate_file to generate new test files of different sizes • Note that you have to process the command line arguments (e.g. file name).