-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileworking.c
75 lines (63 loc) · 1.62 KB
/
fileworking.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
#include <stdio.h>
#include <elf.h>
#include <malloc.h>
#include <assert.h>
#include <errno.h>
#include "fileworking.h"
char* ReadFile (const char* filename)
{
assert (filename);
FILE* file = fopen (filename, "rb");
if (!file)
{
fprintf (stderr, "Error: Unable to open file %s : no such file or directory.\n", filename);
return NULL;
}
long num_bytes = GetFileSize (file);
if (num_bytes == -1)
return NULL;
char* buf = calloc (num_bytes + 1, sizeof (*buf));
if (!buf)
{
fprintf (stderr, "Error: Unable to allocate memory\n");
fclose (file);
return 0;
}
if (fread (buf, sizeof(*buf), num_bytes, file) != (size_t) num_bytes)
{
fprintf (stderr, "Error: During reading something went wrong...\n");
free (buf);
buf = NULL;
}
fclose (file);
return buf;
}
long GetFileSize (FILE* file)
{
// ToDo: Work with files, that pointer != start
assert (file);
fseek (file, 0, SEEK_END);
long num_bytes = ftell (file); // ToDo: returns -1 error fix
if (num_bytes == -1L)
{
fprintf (stderr, "Error: ftell returns -1, error code: %d.\n", errno);
return -1;
}
fseek (file, 0, SEEK_SET);
return num_bytes;
}
int WriteFile (const char* filename, const char* buf, size_t buf_size)
{
assert (filename);
assert (buf);
assert (buf_size);
FILE* file = fopen (filename, "wb");
if (!file)
{
fprintf (stderr, "Error: Unable to open file %s : no such file or directory.\n", filename);
return 1;
}
fwrite (buf, sizeof (*buf), buf_size, file);
fclose (file);
return 0;
}