| CompOrg Fall 2001 |
|   Assignment   |   Stuff you need to know   |   What and How to submit   |   Grading   |   Notes and Hints   |   FAQ |
|
Assignment |
Write a C program that gets the name of a file from the command line (as a command line argument to the program), reads in the file and outputs the contents of the file in hex and ASCII, with 8 bytes of the file printed on each line. The output format must match the description given below exactly!
Each line of output describes 8 bytes of the file. Each line should consist of 3 parts:
First comes a 4 hex digit number that specifies the offset of
the bytes shown (where in the file these bytes are found). This offset
should be 0000 for the first line, 0008 for
the next line, etc. This limits the file size to 64K bytes, since we
would need more than 4 hex digits to represent an offset larger than
64K. You don't need to worry about files larger than 64K bytes in
size.
The hex value of each of the 8 bytes follows, with the first byte being the leftmost in the output. Each byte from the file should be represented by a 2 hex digit number. There must be a space following each byte value (each byte from the data file should be shown as 2 hex digits followed by a space).
The ASCII character represented by each of the 8 bytes. Unprintable characters should be changed to the character '.', so that things like newlines or control characters found in the file show up as '.' in the output. You can assume that any byte whose value as an unsigned integer is less than 32 or greater than 127 is unprintable.
Example: the data file contains the following text:
|
Note that there is a newline (ASCII code
0A) at the end of each line.
Here is the output of the program (the program is named mdump and
the data file is names sample)
:
|
|
Some things you will need to know (if you don't know 'C') |
printf(): check the lecture notes or the man
page for printf. Note that the "%X" format prints an integer value in
hex!
File I/O: specifically the functions fopen(),
fclose(), fread() and feof().
You best source of information is any introductory C text (or web tutorial),
but here is some basic information:
fopen() opens a file and creates a "file handle". The
full prototype is:
FILE *fopen(const char *path, const char *mode)
The value returned is a pointer to a FILE data
structure (defined in stdio.h). You don't need to know much about a
FILE data structure - you juse use it later when calling
other functions like fread(),
fclose(). fopen() returns NULL if
things don't work (perhaps the path is invalid).
path is a string that indicates the pathname of the
file to be opened. If the path does not contains any slashes, then it
references a file in the Current Working Directory.
mode is a string that specifies how the file should be opened.
To open a file for reading use the mode "r".
fread() reads from a file that has already been opened. The
full prototype is:
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
Note: size_t is special integral type
supported by POSIX - you can use an int for this type.
ptr is a pointer to some place in memory where the
data read from the file will be placed. This memory must already be
allocated!
size is the size (in bytes) of each element
that will be read. For this assignment you probably just want to read
individual bytes (each element is a byte), so size would
be 1.
nmemb is the number of elements that should be
read in and stored in memory. For this assignment you probably want to read
in 8 bytes at a time (so nmemb would be 8).
stream is the pointer to a FILE data structure
that was returned by fopen().
The return value is the number of elements read from the
file and placed in memory. If this number is less than the
requested number, this indicates EOF. A return value of 0
may indicate an error, or may indicate there is nothing left to read,
the only way to know is to check using feof().
feof() tells whether and End-of-File (EOF) condition
exists. The full prototype is:
int feof(FILE *stream)
stream is a pointer to a FILE data structure
returned by fopen().
The return value is 1 if an End-of-File condition exists
(there is no more to read from the file), or a 0 if there is
more data available to be read.
fclose() closes a file. The full prototype is:
int fclose(FILE *stream)
stream is a pointer to a FILE data structure
returned by fopen().
The return value is 0 on success, any other value indicates
some kind of error.
|
Submitting |
You should submit your program (the source code for your program),
along with a file named README that contains a
description of the system you used for development (what
kind of machine, what kind of operating system and compiler).
You must also include some sample output, specifically you must run your program on the the two files sample1 and sample2. To get these files you should right-click on the above hyperlinks and select "save as". If you simply view the files in your browser and then save them, you may lose part of the file contents (any byes that happen to have values that are ASCII control/formatting characters can get lost/changed by the browser).
To make sure that you are getting sample input files correctly, we include one sample along with the correct output:
Correct output for the sample data file testdata:
|
Projects must be submitted via email to comporg-submit@cs.rpi.edu. The subject line of the submission message should contain a single number indicating the project number (1 for HW1, 2 for HW2, etc.). You must include your files as attachments, feel free to send a zip-file or a tar file.
Don't send compiled code!You can expect a return email indicating receipt of your project submission immediately. This receipt will include a list of all the files that were successfully extracted by the submission script - please look over the receipt carefully to make sure your submission worked.
Multiple Submissions: You can resubmit up to 10 times for each project, we will always grade the last submission received unless you tell us otherwise.
|
Grading |
We will compile and run your programs using gcc on a Unix computer, so you should include only code that will work in this environment. Specifically, if you develop the code on a PC using Visual C++ or some fancy environment, you need to make sure that it can compile under Unix (or at least using gcc under Windows).
Part of your grade will also be based on the sample output you provide in your submission. Make sure you run your program on the two sample files, save the output of each to a file and submit these files!
Your grade will be determined according to the following formula:
Source Code compiles and produces correct output: 50%
Sample output correct: 30%
Source Code is readable (commenting, structure, neatness, etc.): 20%
You should note that you do get some credit for simply including correct output for the two sample files. This means two things:
If you can't get your program working - you can attempt to build these by hand, although the sample files include non-ASCII data, so you will need to be able to figure out what is in those files.
If we can't get your program to work on a Unix machine, you can still get credit for the sample output.
|
Notes and Hints |
You can do this assignment under Windows using gcc (how we we ever know?), although since you will need to do later programs on a Unix machine you might as well start now...
The Unix "od" command ("Octal Dump") does something very similar to what your program should do (it does a lot more as well). It may be useful to use "od" to make sure your program is working correctly.
Make sure your program works with files that are not just plain text! Binary files (perhaps executable programs) should also work. Try running your program on itself! You should be able to see the string literals (constants) contained in your program. For example, if your program includes the statement:
printf("%4X: ",offset);
you should see the string "%4x: " show up in the output when you run the program on itself.
Check the course homepage for a HW1 FAQ - the will evolve as we get questions.