CS520
Fall 2013
Lab 1
Due Wednesday, September 4
Piazza
I have posted some programs that contain useful code on piazza, but as
of my writing this, only a few students have registered. I would like
all students to register on piazza so I can get responses to my poll
question, which is about how much additional instruction I need to do
for the c programming language. To encourage students to register, I
have made this worth 5 points on this lab.
The Lab Program
Write a program that takes two command line arguments, the first is an
output file name and the second is a number, and writes the number out
in little endian form to the file. One complication is that, in order
to make some aspects of your life easier, the input is in hex. The
good news about having the input in hex is that it will look a lot
closer to the output, which will make testing easier. The bad news
about hex is that you will need to figure out how to parse a number
that is in hex.
Example:
-
./write_int outfile 0
Should create a file called output, such that:
-bash-4.2$ hexdump -C output
00000000 00 00 00 00 |....|
00000004
-
-bash-4.2$ ./write_int output af99ccee
Should create a file called output, such that:
-bash-4.2$ hexdump -C output
00000000 ee cc 99 af |....|
00000004
hexdump is the name of a program that lets you view binary files
viewed as a hex file. It is worth doing a quick experiment with
hexdump to get better acquainted with the program: try running hexdump
on one of your source files (preferably a small one). For me:
cmw@paris:~/cs520/p1/lab2$ hexdump -C write_int.c
00000000 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 69 6f 2e |#include< stdio.|
The -C option is important. Try running hexdump on one of your source
files without the -C option and see what it does. In order to
interpret the output of hexdump, it may help to have access to an
ASCII table, which is very easy to find on the internet. Hint: just
type "ASCII table" into google and select one of the many choices.
Detailed instructions
- Log on to one of the computers in the lab.
- Open up a terminal
- Make a directory entitled lab2
mkdir lab2
- Change to the lab2 directory
cd lab2
- In the lab2 folder, create a file called write_int.c and write
your program in this file. You are free to use any editor you can
find on agate that you are happy with. Many students have been
using SciTE, so if you have no preference for editor, that is a good
place to start.
- To compile your file, in the lab2 directory type:
gcc -g -Wall -o write_int write_int.c
- -g indicates to compile the program with debugging information
- -o hex_int makes the compiler name the resulting binary hex_int
instead of a.out
- When you are done, submit your source file. In order to submit
your file, you will need to be SSH'd in to agate.
~cs520/bin/DoSubmission.py lab2 write_int.c
Submitting
Please submit whatever you have at the end of lab, even if it is
incomplete. You have the rest of today to finish this assignment.
Grading
- 5 points will be awarded for registering for the class piazza. I
would also like you to answer my poll question, but the responses
are anonymous so I can't grade that, but I would still like you to
vote in my poll.
- 30 points will be awarded for being able to read in a valid
number from the command line.
You should reach this point within 15 minutes. If you do
not, please check the board for code regarding how to do this.
- 10 points will be awarded for being able to create a file with
the correct name.
- 15 points will be awarded for being able to write something to
the file.
You should reach this point within 30 minutes. If you do
not, please check the board for code regarding how to do this.
- 30 points will be awarded for being able to write the correct
data to the file.
You should reach this point before lab ends.
- 10 additional points will be awarded for detecting different
kinds of invalid input. Examples of invalid input are, but are not
limited to:
- Something that isn't a number
- Number that is too big
- File that can't be written to
This is part of the assignment so that all students have something
to do, even ones who finish the other parts of the assignment
quickly. If you find that you are unable to reach this point, don't
worry about it too much, because it isn't worth very much. Error
checking, however, is something that I will be doing on your
assignment, so learning how to identify and react to different kinds
of error conditions is important.
Stuff you may find helpful
Reading an Integer in from the command line
As I am sure you remember, part of last lab was to read in a number
from the command line. If you are happy with your code for doing
this, you should copy it! If you didn't do very well with reading an
integer in from the command line, you can feel free to copy some of my
code from lab 1, which is currently posted on piazza. You will find
that learning how to splice programs together is an extremely useful
skill.
Writing to a file
Another useful resource is stackoverflow.com. This website, while it
does have its problems, is often quite useful. For example, this
question
shows much of the basics of what you need to do. As usual, you need
to make sure you divine the correct lesson out of this, the person's
code is fairly close to what you will need to do, except it contains
one error. Fortunately, the first answer says what the error is, and
how to correct it.
Writing to a file in little endian
As was previously mentioned, this program is very closely related to
the previous lab, where you wrote something out to the command line in
little endian. If you are still unsure about precisely how you should
accomplish this, you should look at your code for lab 1, or my code
from lab 1 if your lab 1 had problems. Instead of writing to stdout,
you can instead write to the file in binary.
Although there are a number of ways to write to a file, I found the
function putc(int
character, FILE* stream) to be useful for doing this.