Write a c program that reverses bytes of a given arbitrary


Assignment

1.

(a) Write a C program that reverses the bytes of a given arbitrary length file and outputs them to another specified file, which should be overwritten if it already exists.

So, invoking your program as follows

./reverse infile outfile

will result in the first byte of outfile being the last byte of infile, and so on until the last byte of outfile is the first byte of infile. The given filenames are just an example, make your own infile. You must use fseek()/fread()/fwrite().

(b) Write another C program that reverses the bytes of a given arbitrary length file in-place, as in, the contents of the file are swapped without a temporary file and without producing a second file. Use mmap(). E.g. ./reverse2 infile results in the contents of infile being reversed after the call, and calling ./reverse2 infile twice should result in the same file as you started with.

(c) Compare the performance of ./reverse with ./reverse2. Use the timing function below to target your timing to just the portion of the program which actually does the reversing and produces output. Get the averages of a bunch of runs.

#include
#include
#include
// you may use this to convert the contents of the timeval struct to ns long nanosec(struct timeval t){
return((t.tv_sec*1000000+t.tv_usec)*1000);
}
int main(){
int res; struct timeval t1, t2;
res=gettimeofday(&t1,NULL); assert(res==0); //stuff you want to measure might go here res=gettimeofday(&t2,NULL); assert(res==0);
//find average time here
}

2. Write a wc utility that takes in a single filename as an argument, and computes the number of characters, words, and lines present in that file. Now, have this wc store the name of the input file, and numbers of characters, words, and lines in a binary file (put all the information you're going to save into a struct, then write that struct out to a file); call the file wc.saved. Each time this wc is invoked from the command line, and only if the input file exists and is readable, use explicit file I/O to add the file's information to wc.saved if that file's information is not already present in wc.saved. If information about the file already exists use mmap to edit its entry to reflect the latest counts. Add a -p option to your program so that invoking wc -p prints all the collected data out of wc.saved (reasonably formatted, of course).

Solution Preview :

Prepared by a verified Expert
Database Management System: Write a c program that reverses bytes of a given arbitrary
Reference No:- TGS02515085

Now Priced at $35 (50% Discount)

Recommended (93%)

Rated (4.5/5)