-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathio.c
49 lines (42 loc) · 980 Bytes
/
io.c
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
/*
* Řešení IJC-DU2
* Autor: Tomáš Matuš, FIT VUT Brno
* Login: xmatus37
* Datum: 19.4.2021
* Přeloženo: gcc 10.2.0
*/
#include <stdio.h>
#include <ctype.h>
#include "io.h"
#include "htab.h"
int read_word(char *s, int max, FILE *f) {
if (f == NULL) {
fprintf(stderr, "File returns NULL\n");
return -1;
}
int length = 0;
int c;
// read extra white-spaces and ignore them
while ((c = fgetc(f)) != EOF && isspace(c)); // empty body
if (c == EOF) {
return EOF;
}
s[0] = (char)c;
length = 1;
for (; length < max - 1; length++) {
c = fgetc(f);
if (isspace(c)) {
break;
}
s[length] = (char)c;
}
// add end of string
s[length] = '\0';
// too long word
if (length == max - 1) {
while (isspace(c = fgetc(f)) == 0) { // isspace() returns nonzero when c is white-space
length++;
}
}
return length;
}