Help Using C for Assignment 8

Help Using C for Assignment 8

How to use the compiler

gcc is located in /usr/local/bin/. This should be in your path already. First, create a text file containing your code, say "hw8.c". To compile it to an executable, say "simulation", run gcc as follows:

gcc hw8.c -o simulation
This will either compile to the output file or give you various error messages. To find out more about gcc, you can read the man page.

How to use the debugger

gdb is located in /usr/local/bin/. This should be in your path already. Let's say you have an executable, "simulation", from source code, "hw8.c". You invoke gdb as

gdb simulation
You can then set breakpoints and step through it and such as you would expect. If your code core dumps, you can invoke gdb as
gdb simulation core
From here, you can determine what was going on at the moment that it core dumped. Use "bt" (backtrace) to display the call stack. Use "up" to go up one level in the call stack and see the state there. To find out more about gdb, you can read the man page.

How to take command line input, such as "-verbose"

Normally, your main function looks something like

void main() {
blah blah blah
}
Alternatively, you can have main take two arguments to indicate how your program was invoked.
void main(int argc, char **argv) {
blah blah blah
}
argc will indicate the number of terms it was invoked with; argv will be an array of strings containing those terms. So, if your code is invoked as "hw8", then argc==1, argv[0]=="hw8". If your code is invoked as "hw8 -verbose", then argc==2, argv[0]=="hw8", argv[1]=="-verbose".

How to get input from the trace file

There are several ways this can be done. I am going to explain one of them that I find relatively simple. You should declare a file pointer.

FILE *fptr;
Then you can open the file.
fptr = fopen("trace.in", "r")
Now you should have some kind of loop reading each line and doing something with it. To read a line, you can use fscanf, which takes a format much like printf.
fscanf (fptr, "%c %c %d\n", &IDchar, &RWchar, &address);
Make sure that the arguments being read into are pointers to chars and ints, not actually chars and ints themselves. When you have read the entire file, fscanf will return EOF. So at this point, you will probably break out of the loop.
if (EOF==fscanf (fptr, "%c %c %d\n", &IDchar, &RWchar, &address))
	break;
To close the file now, simply use fclose.
fclose(fptr);
To use all of these, you must include the standard I/O library. At the top of your code, include the line:
#include <stdio.h>
You can get more info on fopen, fscanf, and fclose from man pages.