Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this assignment, you will use the C functions fopen, fclose, fseek, ftell, fread, fwrite, feof, etc. to do file processing. The input to your

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed
For this assignment, you will use the C functions fopen, fclose, fseek, ftell, fread, fwrite, feof, etc. to do file processing. The input to your program will be a file full of records containing some sort of information. Your program's task is alter the contents of the file so that the records are now stored in reverse order. The contents of each individual record are still presented in the same order as before, but the order of the records themselves is reversed. For example, recall the simple "database" of Person structs that we considered in the class lessons. $ cat db.txt ann F 1998 bob M 2000 sue F 2003 tom M 1997 Of course, this file is really stored as a binary file (not a text file) as shown below. $ od -An -x -w8 -endian=big db.bin 616e 6200 4600 ce07 626f 6200 4do0 d007 7375 6500 4600 d307 746f 6d00 4d00 cd07 This file db.bin contains four records, and each record has a size of eight bytes. Executing your program should alter the file db.bin, whose contents should now be: $ od -An -x -w8 --endian=big db.bin 746f 6100 4d00 cd07 7375 6500 4600 d307 626f 6200 4do0 d007 616e 6200 4600 ce07Notice that this file contains no newline characters. Each record appears on a separate line above only because the -w8 option was used in the od command. Note also that the file may have any number of records, unlike the code presented in the class notes, which assumed that the db.bin file contained exactly four records. Your program should work for a file with any number of records (including zero!). To accomplish this task, your program will read data (using fread) into main memory (RAM), and then write that data back out to the file in the correct location in that file. Note that your program is altering the original file and not creating a second file. Files are stored on secondary memory (aka storage, e.g., hard disk drive). Any read/write to storage is very slow, compared to manipulating variables in main memory. Since each file read is slow, the OS optimizes system performance by reading an entire page from storage. For example, suppose you want to eat a sandwich for lunch. You could drive to the grocery store, buy a loaf of bread, and then drive home. You could then drive to the grocery store again, buy some mustard, and then drive home. You could then drive to the grocery store again, buy some ham, and then drive home. You could then make your sandwich and eat it. Clearly, it would be more efficient to make only one trip to the grocery store. The same idea applies to accessing data in storage. You can execute char foo; fread ( &foo, 1, 1, someFilePtr ); to read a single char/byte into the variable foo. But since you are going all the way out to storage anyway, you might as well read an entire page of data. For example, the page size on our Unix host is 4,096 bytes. Your program will take the following input on the command line. argument argv[1] indicates the size (number of bytes) in each record in the file (e.g., 8 bytes for the simple db.bin file) argument argv[2] indicates the page size, in bytes, for the system you are using argument argv[3] is the name of the file that you are altering For example, invoking your program might look like: $ /a.out 20 1000 myInputFile In this example, each record in myInputFile file contains 20 bytes, and each page can hold 1,000 bytes. Therefore, each fread operation will fetch in 50 records. For simplicity, we will assume that the record size evenly divides the page size. Your program must minimize the number of reads/writes to the file system by fetching data in pages, instead of one record at a time. Every argument on the command line is a string. You can convert a string into an integer using the "ASCII to Integer" function atoi in stdlib.h, as follows:int foo = atoi( argv[1] ); Consider the following example input file called myInput: ABCdefGHIjkIMNOpqrSTUvwx This file contains exactly 24 bytes (no newline chars). If your program is invoked as: $ /a.out 3 6 myInput Then the resulting output should be the 24 bytes: vwxSTUpqrMNOjklGHIdefABC If your program is invoked as: $ /a.out 3 12 myInput Then the resulting output should be the same 24 bytes: vwxSTUpqrMNOjkIGHIdefABC If your program is invoked as: $ /a.out 6 12 myInput Then the resulting output should be the 24 bytes: STUvwxMNOpqrGHIjklABCdef In this tiny example file myInput, the "record" is very small and truly meaningless. But C sees every file as simply a stream of bytes. Whether or not those bytes make any sense is completely irrelevant to the C program.If your program is executed with any of the following arguments: $ /a.out 3 3 input $ /a.out 3 6 input $ /a.out 3 9 input Then the input file should have been altered to exactly match the file:Your program must not use any storage beyond the original file. In other words, your program must operate "in place" with respect to storage. Recall that Bubblesort and Heapsort are both "in place" algorithms, but Merge Sort is not

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions