-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.c
51 lines (41 loc) · 1.08 KB
/
input.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
#include <stdio.h>
#include "input.h"
#include "eli.h"
#define BUFFER_SIZE 1024
// Global variables.
char line[BUFFER_SIZE];
char *linePointer = line;
FILE *inputFile;
// Load given input file and returns its size in bytes.
// In case of error program is terminated with EXIT_FAILURE value.
// File pointer is saved into global variable.
int initInput(const char* fileName) {
int size;
if ( !fileName )
error("No input file.");
inputFile = fopen(fileName, "rt");
if ( !inputFile )
error("Cannot open given file.");
// get the file size
fseek(inputFile, 0, SEEK_END);
size = ftell(inputFile);
fseek(inputFile, 0, SEEK_SET);
return size;
}
// Read one character from the input file.
// Returns character of EOF (in case of EOF or error)
int getChar() {
if (!*linePointer) {
if (!fgets(line, BUFFER_SIZE, inputFile)) {
fclose(inputFile);
return EOF;
}
linePointer = line;
}
return *linePointer++;
}
// Reset file pointer to the beginning of the given file.
void resetFilePtr(void) {
*linePointer = 0;
fseek(inputFile, 0, SEEK_SET);
}