CS 3113: Project 1
Objectives
The objectives of this project are for you to:
- Gain experience in the representation of chars, ints, floats
and strings at the byte level, including translation between
these representations.
- Implement low-level file reading and writing, as well as
positioning of the file offset at a specified location.
Overview
- Your program will create a 112-byte buffer that it stores in
memory. This buffer should be declared as an unsigned
char array.
- Through a text interface, the user is able to:
- Store characters, strings, ints, floats into specified
locations in the buffer
- Read characters, strings, ints, floats from specified
locations in the buffer
- File storage:
- A file, specified at the command line, will store data
in blocks of 112 bytes (the blocks are contiguous,
meaning that block 0 corresponds to bytes 0..111 in the
file, block 1 corresponds to bytes 112..223, ...)
- The user can request that a specific block is read from
the file and into the buffer
- The user can request that the buffer be written to a
specific block
- If the file does not exist, then it is created
- You may assume that user requests will never be beyond
the available bytes in the file (except possibly to
write a block just beyond the end of the file). The
implication is that you don't need specific logic in
your code to reason about file length vs which block you
are reading from or writing to.
Proper Academic Conduct
The code solution to this project must be done individually. Do not
copy solutions to this problem from the network and do not look at or
copy solutions from others. However, you may use the net for
inspiration and you may discuss your general approach with others.
Specification
Your main program maintains a 112-byte buffer in memory. The user can
interact with this buffer by placing integer, floating point,
character, and string values at a specified locations in
the buffer. The user may request that integers be
incremented/decremented and that strings be appended to. In addition,
the user may request that the contents of the
buffer be written to a specified block in a binary file, or that
the buffer values be read from a specific block in the file.
The command-line interface to your program is as follows (note that
you must respect the casing):
./project1 [FNAME]
This program does the following:
- Opens the file specified using argument one. If no argument is
specified, then use the file "data.bin".
- Sitting in a loop, it accepts one of several commands from the
user and executes these commands. The loop will continue until the end-of-file marker is received from stdin
- Each command is exactly one input line, terminated by a newline
character.
- The commands manipulate or read the contents of the buffer, or
they transfer values between the buffer and the file.
- It is an error if a command has an incorrect number of
arguments or incorrect arguments.
- When there are no more commands (EOF), then the program closes
the file and exits.
- All errors are written to STDERR.
Parsing Lines
Use the same technique to tokenize the individual lines as you did in project 0.
Commands
There will be exactly one command per line of input from STDIN.
- Dump the contents of the buffer.
d
Print each byte in hexadecimal format. There are 16 bytes per
output line, for a total of 7 lines. Each byte has exactly
two characters, and bytes are separated by a space.
- Zero out the contents of the buffer.
0
Set all of the byte values in the buffer to zero.
- Write a hexadecimal byte value to a buffer location.
H LOC VAL
LOC is the location in the buffer to write to (expressed in
decimal). VAL is the byte
value (in hexadecimal) to be written
- Read a byte value from a buffer location.
h LOC
LOC is the location in the buffer to read from. Prints the
value of the byte (in hexadecimal)
- Write a character to a buffer location.
C LOC VAL
LOC is the location in the buffer to write to. VAL is the
character to be written
- Read a character value from a buffer location.
c LOC
LOC is the location in the buffer to read from. Prints the
value of the character (i.e., the glyph)
- Write an integer value to a buffer location.
I LOC VAL
LOC is the location in the buffer to write to (remember that
integers require multiple bytes to store). VAL is the integer
value (in decimal) to be written
- Read an integer value from a buffer location.
i LOC
LOC is the location in the buffer to read from. Prints the
value of the integer (in decimal)
- Write a floating point value to a buffer location.
F LOC VAL
LOC is the location in the buffer to write to. VAL is the
floating point value to be written
- Read a floating point value from a buffer location and print it
out.
f LOC
LOC is the location in the buffer to read from. Prints the
value of the floating point value (use default floating point
formatting ("%f")).
- Write a string to a buffer location (may be multiple characters).
S LOC VAL
LOC is the location in the buffer to write to. VAL is the
string value to be written. Note that it will contain no
spaces (due to the tokenization process). Make sure that the
NULL terminator is also written to the buffer.
- Read a string from a buffer location.
s LOC
LOC is the location in the buffer to read from. Prints the
value of the string.
- Append a string to a string at a buffer location (may be multiple characters).
A LOC VAL
LOC is the location in the buffer of the original string. VAL is the
string value to be be appended. Note that it will contain no
spaces (due to the tokenization process). Make sure that the
NULL terminator is also written to the buffer.
- Write the contents of the buffer to the specified file block.
w BLOCK
BLOCK is the block number to write to.
- Read the contents of the specified block in the file into the buffer.
r BLOCK
BLOCK is the block number to read from.
- Increment the integer at a buffer location
+ LOC
LOC is the starting location in the buffer that contains the integer.
- Decrement the integer at a buffer location
- LOC
LOC is the starting location in the buffer that contains the integer.
Notes
- You may assume that the user will not specify a buffer location
that is outside the 112-byte buffer (and multi-byte values will
not extend past the end of the buffer).
- You may assume that the user will provide arguments in the
correct numerical range.
- All program input is from STDIN.
- All normal program output is to be placed in STDOUT.
- You must address the case of incorrect numbers of arguments or
of parse errors by writing an error to STDERR.
Examples
Program Invocation (for all examples)
./project1
Input Text
0
d
Output Text
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Input Text
0
I 27 67
d
i 27
h 27
c 27
s 27
Output Text
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 43 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
67
43
C
C
Input Text
0
S 20 Hello,
s 20
d
c 24
i 24
h 24
H 26 20
A 20 World!
d
s 20
Output Text
Hello,
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 48 65 6c 6c 6f 2c 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
o
11375
6f
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 48 65 6c 6c 6f 2c 20 57 6f 72 6c 64
21 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Hello, World!
Input Text
0
I 32 1138
i 32
i 33
i 34
h 32
h 33
h 34
+ 32
d
+ 32
i 32
d
Output Text
1138
4
0
72
4
0
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
73 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1140
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
74 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Input Text
0
d
w 0
S 30 Baumwipfelpfad
i 0
d
w 1
w 2
r 0
i 0
d
I 20 1138
i 0
d
w 3
r 2
i 0
d
r 3
i 0
d
Output Text
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 42 61
75 6d 77 69 70 66 65 6c 70 66 61 64 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 72 04 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 42 61
75 6d 77 69 70 66 65 6c 70 66 61 64 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 72 04 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Submitting Your Program
- The deadline for submission is Monday, Sept. 28th at
11:45:00 pm.
- Bonus: if you have your d, 0, h, H, i and I commands working
by Wednesday, Sept. 23rd (11:45:00 pm), then you can receive up to 5%
additional points. To have this count, you must submit your
project by the early deadline to the project1-bonus
Gradescope area.
- Your submission is composed of the following files, named exactly as shown (including casing):
- project1.c: your source file
- Include your name and project number at the top of the file
- Makefile: the makefile that implements:
- all: compiles your program (this is the default behavior)
- clean: deletes the executable file (project1) and any intermediate files (.o, specifically)
- README.txt: documentation
- Include your name and project number at the top of the file
- Document any Internet resources that you consulted (URLs). If there are none, then
explicitly state this. These must be specific URLs
that we can use to see the same pages that you
are using. If there are none, then
explicitly state this.
- Document any peer class members that you
discussed your solution with. Note that you may not
look at or share code that solves this specific
problem. If there are none, then
explicitly state this.
- Documentation requirements: function-level and inline
documentation are important components of writing code. They help
you to organize your thoughts while programming and help to
communicate the method and the intent of the code to your future
self or to collaborators (of course, you will not have collaborators
in this class). While we will not be specifically grading
documentation in this class, we will not be able to comment on your
code unless it is sufficiently documented. This will be true whether
you are asking for help before the submission deadline or looking
for feedback on your solution after the deadline. In short, you
should take the time to properly document your code as you develop
it.
- Submit your files:
- Create a zip file named project1.zip containing
your source code (c and h files), Makefile and README.txt file. The
following Linux shell command will do this for you:
zip project1.zip *.c *.h Makefile README.txt
Note I: this is a good thing to add to your Makefile.
Note II: do not include object files, executables or
temporary files in your zip file.
- Submit your zip file to Gradescope (access via Canvas).
Shortly after your submission, you will receive
automated feedback and a grade for the
correctness component of your grade.
- You may submit multiple times until the deadline.
- To be counted as on time, your solution must be submitted to
the Gradescope server by 11:45:00
pm on Monday, Sept. 28th. Grade penalties will be imposed for
late submissions (see the syllabus for the details).
Grading Criteria
- README.txt (10%): contains the required information
- Makefile (10%): satisfies the make file requirements
- Correctness (80%): passes all of the (hidden) unit tests
Downloads
The following file contains a number of example tests: project1_dist.tar
Testing
The testing procedure is the same as in Project 0.
Hints
- Remember that characters use the ASCII encoding. You can take
advantage of this to make your implementation simple.
- Don't implement everything at once. Instead, get the first few
commands written and tested before moving on to the more
complicated commands.
- Any debugging code should only print to STDERR. This way, it
won't interfere with our testing procedures.
- Useful C functions:
- You can ask your VM about any of the above functions. For example, type the following into your shell:
man fgets
- Useful libraries (which define the above functions):
- stdio
- ctype
- string
- stdlib
- You can simulate an end-of-file marker (EOF) at the terminal by typing CTRL-d
Addenda
- 2020-09-17: The user interaction loop continues to process lines until the end-of-file marker is received from stdin.
- 2020-09-17: All locations (LOC) are expressed in decimal.
- 2020-09-17: Printing of floating point values should just use
the default formatting ("%f").
- 2020-09-17: Bonus deadline has been extended by one day (to
Wednesday the 23rd)
- 2020-09-21: Small change made to required elements in the README.txt file.
andrewhfagg at gmail.com
Last modified: Tue Sep 29 10:01:44 2020