summaryrefslogtreecommitdiff
path: root/02-12.md
blob: ce40fa911c4e1e9218cec19f708def75b1aef24e (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
[\<- Notes 02/10](02-10.md)

---

# Files Continued

- 2 Ways: Text or Binary

- Regardless, always open the file with `fopen`:

```
char filename[] = "myFile.txt";
FILE *fp = fopen(filename, "r");
```

- The above example opens the myFile.txt for reading
- The fopen() function takes in two arguments
	1. the filename
	2. the mode
		- "r" -> read
		- "w" -> write
		- "a" -> append
		- "rb" -> read binary
		- "wb" -> write binary
		- "ab" -> append binary

- To read a text file:

```
fscanf(fp, "%d", &x);
```

- The above example reads the file pointed to by fp for a single integer and stores it in x
- The fscanf() function takes in three arguments
	1. the pointer to the file to read
	2. the data type to read for
	3. the memory address of the variable to save what was read

- To write to a text file:

```
fprintf(fp, "%d", x);
```

- The above example writes the integer x to fp
- The fprintf() function takes in two arguments
	1. the pointer to the file to write to
	2. the text to write

- To read a binary file:

```
fread(&x, sizeof(int), 1, fp);
```

- The above example reads 1 int, stores it in variable x from the file pointed to by fp
- The fread() function takes in four arguments
	1. variable address to store what is read
	2. the amount of memory to read
	3. How many times to read the amount of memory specified in the 2nd argument
	4. the file pointer to read from

- To write to a binary file

```
fwrite(x, sizeof(type), 1, fp);
```

- The above example writes x to the file pointed to by fp
- The fwrite() function takes in four arguments
	1. The variable to write from
	2. the amount of memory being written
	3. How many times to write the amount of memory specified in the 2nd argument
	4. the file pointer to write to

- Example: Return the number of integers in a text file greater than 100

```
int count(FILE *fp){
	int number, counter = 0;
	
	while(fscanf(fp, "%d", &number) == 1){
		if(number > 100) counter++;
	}

	return counter;
}
```

- Example: Return the average of all the numbers in a text file

```
int avg(FILE *fp){
	int number;
	int sum = 0;
	int counter = 0;

	while(fscanf(fp, "%d", &number) == 1){
		sum += number;
		counter++;
	}

	return sum/counter;
}
```

- Example: Copy a binary file to another file (100 bytes at a time)

```
FILE *source;
FILE *dest;
char srcname[] = "my_source"; //the name of my file

source = fopen(srcname, "rb");
dest = fopen(srcname, "wb");
if(source == NULL || dest == NULL) return; //there was an error

void copy_bin(FILE *src, FILE *dest){
	int r;
	char x[100]; //store bytes for transfer

	while((r = fread(x, sizeof(char), 100, src)) > 0){
		fwrite(x, sizeof(char), r, dest);
	}

return;
```

- In that last example `r` is used instead of 100 in case there is less than 100 bytes left to read in the file
- Don't forget to `fclose` at the end!

- Example: Write a function to compare two files, line by line

```
void compare(FILE *fp1, FILE *fp2){
	char *r1; //points to x1
	char *r2; //points to x2
	char x1[100]; //store from fp1
	char x2[100]; //store from fp2
	int line = 1; //what line number I'm looking at

	while(1){
		r1 = fgets(x1, 100, fp1);
		r2 = fgets(x2, 100, fp2);
		if(strcmp(r1, r2) != 0){ //not equal
			printf("FILES ARE DIFFERENT on line %d\n", line);
			break;
		}
		line++;
	}

	if(r1 == NULL and r2 == NULL) printf("FILES ARE EQUAL\n");

	return;
}
```