summaryrefslogtreecommitdiff
path: root/02-07.md
blob: 79b85c9fd880de26373fbe493faba5eb0f4e80ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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)