From 89d105f965d422bd15b8c10afd707b4d621ab22a Mon Sep 17 00:00:00 2001 From: loshprung Date: Mon, 10 Feb 2020 11:11:47 -0800 Subject: Post-class 02/10 --- 01-31.md | 4 ++ 02-07.md | 201 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 02-10.md | 91 +++++++++++++++++++++++++++++ 3 files changed, 296 insertions(+) create mode 100644 02-07.md create mode 100644 02-10.md diff --git a/01-31.md b/01-31.md index 4158e08..cdf6bc3 100644 --- a/01-31.md +++ b/01-31.md @@ -93,3 +93,7 @@ else{ - It can be helpful to define a tail pointer in addition to the head pointer - the tail pointer would point to the end of the list + +--- + +[-> Notes 02/07](02-07.md) diff --git a/02-07.md b/02-07.md new file mode 100644 index 0000000..79b85c9 --- /dev/null +++ b/02-07.md @@ -0,0 +1,201 @@ +[\<- Notes 01/31](01-31.md) + +--- + +# Arrays of Linked Lists + +- Linked Lists can be initialized as follow: + +``` +#define NODE struct node + +struct node{ + char name[20]; + int size; + NODE *next; +}; + +struct list{ + NODE *head; + NODE *tail; +}; + +struct list x[4]; +``` + +- Now you have 4 linked lists in `x`, each with a head and tail +- Inserting and deleting should be the same, just specify which element in the `x` array you are inserting/deleting + +--- + +# Files + +- Two kinds of files we will cover: + +## Text Files + +- Colletions of characters saved in a secondary storage +- No fixed size +- End file with `` +- End of lines are marked with `('\n')` + +Example: + +``` +This is a text file! +It has two lines. +``` + +The actual file will look like this: + +``` +This is a text file!It has two lines. +``` + +- All textual **input** and **output** data are actually a continuous **stream** of character codes + - We refer to a data source or destinations as an **input stream** or an **output stream** + - These general terms can be applied to: + - files + - the keyboard + - the screen + - any other source of input data or destinations of output data + +- Keyboard and Screen + - **stdin** + - keyboard's input stream + - **stdout** + - "normal" output stream associated with the screen + - **stderr** + - "error" output stream associated with the screen + - **Streams are treated like text files** + - Their individual components are characters + +- **To read from stdin** + +``` +r = scanf("%d", &num); +``` +or +``` +ch = getchar(); +``` + +- **The characters are read sequentially** + - Consecutive calls to scanf and/or getchar will read **consecutive elements** from the keyboard + - ^D usually represents `` + +- **scanf** + - Returns how many elements were read + - Placeholders define the type + + - Examples: + +``` +%c, %d, %f, %s +``` + + - **For limiting the size of a string** + - %Ns - reads up to N characters (array size should be N+1) + - **For lines** + +``` +r = scanf("%[^'\n']", string_name); +``` + + - Only reads the characters as specified by the placeholder + - Spacers (\n, \t, or spaces) may be left in the buffer from previous calls + - **To clear buffer:** + - fpurge() - erases any input or output buffered in the given stream + - Example: + +``` +fpurge(stdin); +r = scanf("%[^'\n']", string_name); +``` + +- **To read lines** + - fgets(array, SIZE, stdin); + - Includes the \n at the end of the line + - If used after scanf, may need to clear the buffer + +- **To write to stdout** + - `printf("%d", num);` or `putchar(ch);` + +- **The file is written sequentially** + - Consecutive calls to `printf` and/or `putchar` will write **consecutive elements** to the file + +## Working with Text Files + +``` +FILE *infp + +if((infp = fopen("data.txt", "r")) == NULL) printf("cannot open the file dat.txt\n"); //failsafe +``` + +- The file is always open for reading from the beginning + +- To open a text file for reading when the **name is stored in a string:** + +``` +char file_name[50]; +FILE *infp; + +if((infp = fopen(file_name, "r")) == NULL) printf("cannot open"); +``` + +- **To read from the text file** + +``` +ret = fscanf(infp, "%d", &num); +``` +or +``` +ch = getc(infp); +``` + +- The file is read **sequentially** +- **Returned value** + - fscanf -- number of values converted + - getc -- the character read (returns EOF at end of file) + +- Open a text file for writing from the **beginning**: + +``` +char file_name[50]; +FILE *outfp; + +if((outfp = fopen(file_name, "w")) == NULL) printf("Cannot open the file %s\n", file_name); +``` + +- Open a text file for writing from the **end**: + +``` +char file_name[50]; +FILE *outfp; + +if((outfp = fopen(file_name, "a")) == NULL) printf("cannot open the file %s\n", file_name); +``` + +- To **Write** to a text file: + +``` +fprintf(outfp, "%d", num); +``` +or +``` +putc(ch, outfp); +``` + +- The file is written sequentially + - Consecutive calls to fprintf and/or putc will write consecutive elements to the file + +- To **Close** a text file: + +``` +fclose(infp); +fclose(outfp); +``` + +--- + +[-> Notes 02/10](02-10.md) diff --git a/02-10.md b/02-10.md new file mode 100644 index 0000000..9efee8f --- /dev/null +++ b/02-10.md @@ -0,0 +1,91 @@ +[\<- Notes 02/07](02-07.md) + +--- + +# Files Continued + +## Binary Files + +- Use Binary Files when no need for a human to read the file + - Converting information into a stream of characters and back into internal format (in this case) is wasting computational cycles and time + +- Files containing binary numbers that are the computer's internal representation of each file component +- Created by executing a program that stores directly in the computer's internal representation of each file component + +- Actually just a stream of zeros and ones and cannot be read with a text editor + +- Example `char x = 2` in memory is `00000010` +- Example `short x = 2` in memory is `0000000000000010` +- Example `int x = 2` in memory is `00000000000000000000000000000010` + +- To open a binary file for reading: + +``` +FILE *infp; + +if((infp = fopen("data.txt", "rb")) == NULL) printf("cannot open the file data.txt\n"); +``` + +- The file is always open for reading from the beginning +- The file name can also be stored in a string + +- To read from a binary file: + +``` +ret = fread(&x, sizeof(int), 1, infp); +``` + +- The file is read sequentially + - Consecutive calls to fread will read consecutive elements from the file +- The next integer is placed into the variable x +- Function fread returns the number of elements read +- At the end of the file, fread returns -1 + +- To open a binary file for writing from the beginning: + +``` +FILE *outfp; + +if((outfp = fopen("data.txt", "wb")) == NULL) printf("cannot open the file data.txt\n") +``` + +- To open a binary file for writing from the end (append): + +``` +FILE *outfp; + +if((outfp = fopen("data.txt", "ab")) == NULL) printf("cannot open the file data.txt\n") +``` + +- To write to a binary file: + +``` +ret = fwrite(&x, sizeof(int), 1, outfp); +``` + +- The contents of variable x is written to the file +- The file is written sequentially +- function fwrite returns the number of elements written + +- To close a binary file: + +``` +fclose(infp); +fclose(outfp); +``` + +- **Files cannot be written or read unless specifically set to do so** + - cannot read when open to write and vice versa + - must close the file if currently open for reading before opening for writing and vice versa + +List of Functions to Learn/Remember: + +``` +fopen, fclose +fprintf, fscanf +fgets +fread, fwrite +fpurge +fflush +fseek +``` -- cgit