Assignment

102 views 7:45 am 0 Comments March 14, 2023

CIS*3110 Assignment 1
Viewing file contents
Due date: Monday, February 6, 11:59pm
Weight: 20%
1. Description
Write a C program that will interactively display text file contents in either ASCII or hexadecimal
(hex) mode. It will essentially be a combination of the Unix command line utilities
cat and
hexdump (in simplified form), but with a basic user menu.
Use this as a reference for the ASCII table: https://www.asciitable.com/
Note that this assignment uses the standard C notation for hexadecimal numbers. For
example,
0x9 is the hex number with the decimal value 9, and 0x1F – hex number with the
decimal value 31.

ASCII mode
In ASCII mode, the program will simply display the contents. However, not all ASCII characters
are visible – some are (or used to be) used for various control sequences – so your program
must replace them with a blank space in the displayed output.

Specifically, do the following replacements when displaying the file contents:
replace the characters in the range 0x0 0x9, 0xB 0x1F with space (ASCII code 0x20)
replace the characters in the range 0x7F and greater with the character ? (ASCII code 0x3F)
Since you will not be replacing
0xA (Line Feed, or LF), the newlines will be displayed as usual,
and the file should look the same as if you display it with the
cat command.

HEX mode
In hex mode, your output will match how hd (hexdump -C) displays the file contents, 16
characters at a time, though we will make a simplifying change:
The first 7 chars will display the character index in hex
0000000 for the first line – i.e. first 16 characters
0000010 for the second line – i.e. the next set of up to 16 characters. Since we’re starting
with character number 16 (
0x10), the line number is 0000010.
The characters will be displayed one at a time, with a space after each character, and two
spaces after the first eight characters. Unlike
hd, you do not need to display the ASCII
version of these characters incased in
||.
The very last line is always the index of last character+1 – e.g. 0x1f, or 31 in the example
below.
1

For example, given a sample file stuff.txt in the assignment description, the output of your
program should be:

ASCII mode:
1234567789
ABCDEFGhijklmn
11111
HEX mode:
00000000 31 32 33 34 35 36 37 37 38 39 0a 41 42 43 44 45
00000010 46 47 68 69 6a 6b 6c 6d 6e 0a 31 31 31 31 31
0000001f
Program execution
Your program will run interactively as follows. It will start in ASCII mode by default and will
prompt the user with a main menu that displays the following options:
Clearly display the current output mode (ASCII or hex) and asking the user to press
o” to enter a file name,
d” to select display mode
x” exit
If the user presses “d“, ask them to enter the mode – “a” for ASCII, “h” for hex – and display
the main menu
If the user presses “o“, ask for the file name, read it in, then try to open it and do the
following:
Read the entire file into a memory using the Unix open and read system calls. The buffer
must be dynamically allocated with
malloc.
If the file cannot be opened, print “cannot open file X” (X being the file name) and
display the main menu
Display file contents according to the current mode, as described above.
After displaying the contents ask the user for input:
m” to return to the main menu. Remember to close the file descriptor.
x” to exit
In either menu, if the user presses “x“, exit the program. Make sure all file descriptors are
closed and all memory has been freed when you exit.
Make sure you always check for invalid input. If the user enters an invalid key in any menu,
display “invalid input” and re-display the menu.
2. Evaluation
Your code must compile, run, and have all of the specified functionality implemented. Any
compiler errors will result in the
automatic grade of zero for the assignment.
2

Your code will be tested with several text files. Marks will be deducted for:
Incorrect and missing functionality
Deviations from the assignment requirements
File descriptions that you opened yourself and that are still open when your program
terminates – valgrind will be used for checking this
Run-time errors, including infinite loops, crashes, etc.
Compiler warnings
Failure to follow submission instructions
For A1, you will not be penalized for memory leaks and other memory errors reported by
valgrind. However, future assignments will include such penalties. It is strongly
recommended that you test your A1 using
valgrind as you develop it, so you learn how to
use
valgrind and understand its output.
3. Submission
Call your file A1.c and the assignment using Moodle. Submit only the source code in a Zip
archive.
The assignment will be marked using the standard CIS*3110 Docker image provided on the
course website and discussed in class. We will download and install a fresh image, create a
container, and use Docker to run and grade your code.
Your code will be compiled with the flags
-Wall -g -std=c11. Make sure you test your
code accordingly.
This assignment is individual work and is subject to the University Academic Misconduct
Policy.
See course outline for details.
3