University of Calgary

octal dump / command line arguments

Lab: Command Line Arguments & od
Source: Katrin Becker, 1999
Command Line Arguments:
 
Up till now all we have seen is:
 
int main ()
{
   return(0);
}
 
use: ./a.out
 
Most Unix commands are written in C so there must be a way to make our programs look like Unix commands. This is done by passing arguments to main in the command line. Main can take two arguments:
int main (int argc, char* argv[])
 
The names "argc" and "argv" are traditional names; not required but all C/C++ programmers will know what they are if you use these names.
 
argc is a count of the number of arguments on the line
argv is an array of strings (1 string for each argument)
 
example:
g++ -g -o myprog myprog.cc
 
argc = 5
argv[0] = "g++"
argv[1] = "-g"
argv[2] = "-o"
argv[3] = "myprog"
argv[4] = "myprog.cc"
 
You decide how many arguments should be there and in what order they should appear. Most Unix command have some optional 'switches' which always start with "-" and if file names are required, they always come last. again this is tradition rather than a requirement.
 
Note: all arguments are strings; those destined to become numbers must be converted by you inside your program.
 

How to use UNIX od (octal dump)
 
- if you don't know this facility make sure you read the man pages and learn it!
od [options] [file] [[+] offset[. | b]]
 
Octal dump; produce a dump (normally octal) of the named file. File is displayed from its beginning unless you specify an offset (normally in octal bytes)
 
OPTIONS:
-b : display as octal
-c : display as ASCII
-d : display words as unsigned decimal (word = 16 bits)
-D : display words = 32 bits as unsigned
-f : display as 32-bit words as floating point
-F : display as 64 bit floating point
-o : unsigned octal
-O : 32-bit unsigned octal
-s : signed decimal
-S : 32 bit signed decimal
-v : verbose
-x : hex
-X : 32 bit hex
+ : required before offset if file isn't specified
 
Modifiers for offset:
. : offset is decimal
b : offset is 512-byte blocks
Drawing by: Mike Polowick
CPSC 233, January 1999

How to use UNIX diff (report on differences between the two given files)

 
- if you don't know this facility make sure you read the man pages and learn it!
diff [options] [diroptions] file1 file2
Difference: output consists of lines of context from each file, with file 1 flagged by a < symbol and file2 text by a < symbol. context lines are preceded by one of 3 symbols: a (add), c (change), d (delete) which are the operations that would be required to change file1 to file2.
 
OPTIONS:
-b : ignore repeating blanks and end-of-line blanks, treat successive blanks as one
-c : produce output in alternate format, with three lines of context
-h : do a half-hearted comparison; complex differences may not show up
-i : ignore upper case and lower case distinctions
-w : ignore white space
DIROPTIONS:
-l : long format
-r : run diff recursively for files in common subdirectories
-s : report files that are identical



Updated: August 28, 2005 02:00 PM