-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathread_text_file.cpp
50 lines (39 loc) · 909 Bytes
/
read_text_file.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
#include "read_text_file.h"
#include "data_path.h"
#include <stdio.h>
using namespace std;
namespace
{
data_path path;
}
data_path get_text_path() { return path; }
void set_text_path(const data_path & newpath) { path = newpath; }
char * read_text_file(const char * filename)
{
if(path.path.size() < 1)
{
path.path.push_back(".");
path.path.push_back("./data/programs");
path.path.push_back("./data/programs");
}
if (!filename) return 0;
struct _stat stat;
if (!path.fstat(filename, &stat))
{
fprintf(stderr,"Cannot open \"%s\" for stat read!\n", filename);
return 0;
}
long size = stat.st_size;
char * buf = new char[size+1];
FILE *fp = 0;
if (!(fp = path.fopen(filename, "r")))
{
fprintf(stderr,"Cannot open \"%s\" for read!\n", filename);
return 0;
}
int bytes;
bytes = fread(buf, 1, size, fp);
buf[bytes] = 0;
fclose(fp);
return buf;
}