-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFileReader.cpp
58 lines (47 loc) · 949 Bytes
/
FileReader.cpp
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
/*
* FilReader.cpp
*
* Created on: 04/12/2012
* Author: polymorpher
*/
#include"FileReader.hpp"
namespace AliasLDA {
char* FileReader::readFile(const char *filename) {
FILE *fp;
int err;
int size;
char *source;
fp = fopen(filename, "rb");
if (fp == NULL) {
printf("Could not open file: %s\n", filename);
exit(-1);
}
err = fseek(fp, 0, SEEK_END);
if (err != 0) {
printf("Error seeking to end of file\n");
exit(-1);
}
size = ftell(fp);
if (size < 0) {
printf("Error getting file position\n");
exit(-1);
}
err = fseek(fp, 0, SEEK_SET);
if (err != 0) {
printf("Error seeking to start of file\n");
exit(-1);
}
source = (char*) malloc(size + 1);
if (source == NULL) {
printf("Error allocating %d bytes for the buffer\n", size + 1);
exit(-1);
}
err = fread(source, 1, size, fp);
if (err != size) {
printf("only read %d bytes\n", err);
exit(0);
}
source[size] = '\0';
return source;
}
}