User Tools

Site Tools


haas:fall2020:cprog:projects:cbf0

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
haas:fall2020:cprog:projects:cbf0 [2020/09/28 18:34] wedgehaas:fall2020:cprog:projects:cbf0 [2020/09/29 15:02] (current) – [Experiencing xxd] wedge
Line 183: Line 183:
  
 <cli> <cli>
 +system:~/src/desig/cbf0$ ./cbf0 in/sample0.txt
 00000000: 3e 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f  >ABCDEFGHIJKLMNO 00000000: 3e 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f  >ABCDEFGHIJKLMNO
 00000010: 50 51 52 53 54 55 56 57 58 59 5a 3c 0a 5b 61 62  PQRSTUVWXYZ<.[ab 00000010: 50 51 52 53 54 55 56 57 58 59 5a 3c 0a 5b 61 62  PQRSTUVWXYZ<.[ab
Line 194: Line 195:
 00000090: 45 46 3a 48 45 58 41 44 45 43 49 4d 41 4c 0a 29  EF:HEXADECIMAL.) 00000090: 45 46 3a 48 45 58 41 44 45 43 49 4d 41 4c 0a 29  EF:HEXADECIMAL.)
 000000a0: 21 40 23 24 25 5e 26 2a 28 0a 2e 0a              !@#$%^&*(... 000000a0: 21 40 23 24 25 5e 26 2a 28 0a 2e 0a              !@#$%^&*(...
 +</cli>
 +
 +Or, if using a line throttle:
 +
 +<cli>
 +system:~/src/desig/cbf0$ ./cbf0 in/sample0.txt 4
 +00000000: 3e 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f  >ABCDEFGHIJKLMNO
 +00000010: 50 51 52 53 54 55 56 57 58 59 5a 3c 0a 5b 61 62  PQRSTUVWXYZ<.[ab
 +00000020: 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72  cdefghijklmnopqr
 +00000030: 73 74 75 76 77 78 79 7a 5d 0a 30 31 3a 20 20 20  stuvwxyz].01:   
 </cli> </cli>
 =====Detecting Terminal Size===== =====Detecting Terminal Size=====
Line 221: Line 232:
  
 Compile and run the above code to see how it works. Try it in different size terminals. Then incorporate the logic into your hex viewer for this project. Compile and run the above code to see how it works. Try it in different size terminals. Then incorporate the logic into your hex viewer for this project.
 +
 +=====Command-Line Arguments=====
 +
 +====setting up main()====
 +To accept (or rather, to gain access) to arguments given to your program at runtime, we need to specify two parameters to the main() function. While the names don't matter, the types do.. I like the traditional **argc** and **argv** names, although it is also common to see them abbreviated as **ac** and **av**.
 +
 +Please declare your main() function as follows:
 +
 +<code c>
 +int main(int argc, char **argv)
 +</code>
 +
 +There are two very important variables involved here (the types are actually what are important, the names given to the variables are actually quite, variable; you may see other references refer to them as things like "ac" and "av"):
 +
 +  * int argc: the count (an integer) of tokens given on the command line (program name + arguments)
 +  * <nowiki>char **argv</nowiki>: an array of strings (technically an array of an array of char) that contains "strings" of the various tokens provided on the command-line.
 +
 +The arguments are accessible via the argv array, in the order they were specified:
 +
 +  * argv[0]: program invocation (path + program name)
 +  * argv[1]: our first argument
 +  * argv[2]: second argument
 +  * argv[3]: third argument
 +  * ...
 +  * argv[N]: Nth argument
 +
 +Additionally, let's not forget the **argc** variable, an integer, which contains a count of arguments (argc == argument count). If we provided argv[0] through argv[4], argc would contain a 5.
 +
 +===example===
 +For example, if we were to execute a program as follows:
 +
 +<cli>
 +$ ./program word 73 another word
 +</cli>
 +
 +We'd have:
 +
 +  * <nowiki>argv[0]</nowiki>: "./program" 
 +  * <nowiki>argv[1]</nowiki>: "word"
 +  * <nowiki>argv[2]</nowiki>: "73" (note not the integer number 73, but the string "73")
 +  * <nowiki>argv[3]</nowiki>: "another" 
 +  * <nowiki>argv[4]</nowiki>: "word" 
 +
 +and let's not forget:
 +
 +  * argc: 5   (there are 5 things, argv indexes 0, 1, 2, 3, and 4)
  
 =====Loops===== =====Loops=====
haas/fall2020/cprog/projects/cbf0.1601318064.txt.gz · Last modified: 2020/09/28 18:34 by wedge