1 / 12

Tutorial 7

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

isi
Télécharger la présentation

Tutorial 7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Tutorial 7 • Binary files (Random Access files) in C • fread/fwrite/fseek/etc. • Command line arguments

  2. 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

  3. 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

  4. Main functions • fopen • fclose • fread • fwrite • fseek • ftell

  5. 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

  6. 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.

  7. 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);

  8. 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?

  9. 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

  10. 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.

  11. 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.

  12. 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).

More Related