-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.c
75 lines (65 loc) · 1.8 KB
/
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
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
// io.c
// Řešení IJC-DU2, příklad 2), 19. 4. 2022
// Autor: Matyáš Strelec, FIT
// Přeloženo: clang 13.1.6
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <string.h>
#include "io.h"
// Read one word from file into given array, returns number of characters read, if word is longer than max, shorten it, expect file ending with \n
int read_word(char *s, int max, FILE *f)
{
char word[max+1]; // Store word in this array
memset(word, '\0', max+1); // Set string to \0
int character; // Currently read character
int count = 0; // Character count
bool allRead = false;
static bool errorPrinted;
// Read a character
character = fgetc(f);
// Skip all whitespace characters
while (isspace(character))
{
character = fgetc(f);
}
// Read word
for (int i = 0; i < max; i++)
{
// End if end of word or file is reached
if (isspace(character) || character == EOF)
{
word[count] = '\0';
allRead = true;
break;
}
else
{
word[count] = character;
count++;
character = fgetc(f);
}
}
// If word is longer than max, shorten it
if(!allRead)
{
word[max] = '\0';
if(!errorPrinted)
{
fprintf(stderr, "Word too long. Continuing with words shortened to %d characters (excluding '\\0').\n", max);
errorPrinted = true;
}
// Skip all remaining characters of the word
do
{
character = fgetc(f);
}
while (!isspace(character));
}
// Copy the word to the destination array and return number of characters
strcpy(s, word);
if (character == EOF)
return EOF;
else
return count;
}