-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio.hpp
More file actions
48 lines (41 loc) · 973 Bytes
/
io.hpp
File metadata and controls
48 lines (41 loc) · 973 Bytes
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
#ifndef IO_H
#define IO_H
#include "global.h"
#include <fstream>
#include <string.h>
#include <exception>
void readfile(const char *filename, std::string &file)
{
class file_exception: public std::exception
{
virtual const char* what() const throw()
{
return strerror(errno);
}
} fileex;
FILE *fh = fopen(filename, "rb");
if ( fh == NULL )
{
//error: No such file or directory
//am i a joke to you?
//I should have read in between the lines ...
throw fileex;
}
fseek(fh, 0L, SEEK_END);
const size_t& length = ftell(fh);
/*it's*/ rewind(fh); //timeeee
file.resize(length, 0);
fread(file.data(), length, sizeof(char), fh);
if(ferror(fh) || feof(fh))
{
throw fileex;
}
fclose(fh); fh = NULL;
}
void writefile(const char *filename, std::string &data)
{
std::ofstream file(filename);
file << data;
file.close();
}
#endif // IO_H