This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
haas:fall2017:cprog:projects:cbf0 [2017/09/17 18:56] – [Experiencing xxd] wedge | haas:fall2017:cprog:projects:cbf0 [2017/10/15 20:49] (current) – wedge | ||
---|---|---|---|
Line 3: | Line 3: | ||
< | < | ||
</ | </ | ||
- | |||
- | ~~TOC~~ | ||
======Project: | ======Project: | ||
Line 35: | Line 33: | ||
The computer works in units of **bytes**, which these days means groups of 8 bits. C has the ability to arbitrarily read and write individual bytes of data, and we will want to make use of that to aid us in our current task. | The computer works in units of **bytes**, which these days means groups of 8 bits. C has the ability to arbitrarily read and write individual bytes of data, and we will want to make use of that to aid us in our current task. | ||
+ | |||
+ | =====Opening and reading from files===== | ||
+ | The nice thing about C is that it tends to embody the " | ||
+ | |||
+ | What this means, basically, is that interacting with data in a file is really no different that interacting with data from the keyboard or data to the screen. We merely need a FILE pointer and appropriate resources allocated. | ||
+ | |||
+ | To interact with a file, we must first declare a pointer to type FILE that will be our point of transaction. | ||
+ | |||
+ | Common names for our file pointer variable are **fp**, **fPtr**, **input**, **inp**, but in reality can be anything you want. | ||
+ | |||
+ | The intention is that, of course, you name variables so they are meaningful in the context of the overall implementation. | ||
+ | |||
+ | <code c> | ||
+ | FILE *input | ||
+ | </ | ||
+ | |||
+ | ====Opening a file with fopen()==== | ||
+ | To attach a file stream to a FILE pointer, we utilize a file opening function such as **fopen()**. | ||
+ | |||
+ | It takes two arguments: | ||
+ | - the path and name of file we wish to open (provided as a string) | ||
+ | - the mode we wish to open the file as (provided as a string) | ||
+ | |||
+ | There are 3 common file opening modes (and combinations thereof, among others, sometimes dependent on the particular operating system being run). For now, I highly recommend just sticking to ONE mode of operation per FILE pointer. This can avoid messy things like data corruption and indirect logic/ | ||
+ | |||
+ | The 3 file modes: | ||
+ | * r - open file for reading (start at beginning) | ||
+ | * w - open file for writing (start at beginning) | ||
+ | * a - open file for appending (add to end) | ||
+ | |||
+ | If we wanted to open the file " | ||
+ | |||
+ | <code c> | ||
+ | input = fopen (" | ||
+ | </ | ||
+ | |||
+ | Note the double quotes around each argument. They both need to be strings (ie array of char terminated with NULL terminator characters), | ||
+ | |||
+ | ====Reading from the file==== | ||
+ | If the file is filled with a set format of data you'd like to retrieve, such as one short integer per line (basically, a text file filled with numbers), we can just use our trusty and familiar **fscanf()** function. We merely have have to indicate the correct file pointer: | ||
+ | |||
+ | <code c> | ||
+ | short int value = 0; | ||
+ | | ||
+ | ... | ||
+ | | ||
+ | fscanf (input, " | ||
+ | </ | ||
+ | |||
+ | If there is no simple universal " | ||
+ | |||
+ | The **fscanf()** function is still viable here, but if all we're after is a char value, there' | ||
+ | |||
+ | To read a byte of data from a file and store it in our variable (called byte), we would do the following: | ||
+ | |||
+ | <code c> | ||
+ | char byte = 0; | ||
+ | | ||
+ | ... | ||
+ | | ||
+ | byte = fgetc (input); | ||
+ | </ | ||
+ | |||
+ | The **fgetc()** function takes the intended FILE pointer it is to read from as its argument, so **input** should be a FILE pointer AND should have previously been **fopen()**' | ||
+ | |||
+ | To make things easier, placing logic to read from a file in a loop can be a very powerful combination. | ||
=====Task===== | =====Task===== | ||
Line 112: | Line 176: | ||
* The hex values and rendered ASCII displayed will be sourced from the file specified on the command-line. While the target files for this project are less than 512 bytes, your program should be able to handle larger and smaller files, and update its display accordingly. | * The hex values and rendered ASCII displayed will be sourced from the file specified on the command-line. While the target files for this project are less than 512 bytes, your program should be able to handle larger and smaller files, and update its display accordingly. | ||
* If a line throttle is given, your program is to stop output of data and ASCII rendering at that line, once it completes. | * If a line throttle is given, your program is to stop output of data and ASCII rendering at that line, once it completes. | ||
- | * Once the data in the file has been exhausted, you need to wrap up as appropriate; | + | * Once the data in the file has been exhausted, you need to wrap up as appropriate; |
* Don't forget to **fclose()** any open file pointers! And **free()** any **malloc()**' | * Don't forget to **fclose()** any open file pointers! And **free()** any **malloc()**' | ||