diff options
Diffstat (limited to '02-07.md')
-rw-r--r-- | 02-07.md | 201 |
1 files changed, 201 insertions, 0 deletions
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 `<eof>` +- End of lines are marked with `('\n')` + +Example: + +``` +This is a text file!<newline> +It has two lines.<newline><eof> +``` + +The actual file will look like this: + +``` +This is a text file!<newline>It has two lines.<newline><eof> +``` + +- 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 `<eof>` + +- **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) |