-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.c
42 lines (32 loc) · 868 Bytes
/
file.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include "file.h"
// open the file, exit if error during opening
FILE* open_file_for_read(const char* file_name){
FILE* fp = fopen(file_name, "rb");
assert(fp != NULL);
return fp;
}
// open the file for writing
FILE* open_file_for_write(const char* file_name){
FILE* fp = fopen(file_name, "wb");
assert(fp != NULL);
return fp;
}
// close the file
FILE* close_file(FILE* fp){
fclose(fp);
fp = NULL;
return fp;
}
// get the file size, return in long
// rewind the pointer
long file_size(FILE* fp){
assert(fp != NULL);
fseek(fp, 0L, SEEK_END); // locate to the end of the file
long result = ftell(fp); // return the file size relative to the beginning
fseek(fp, 0L, SEEK_SET); // same as rewind(fp)
return result;
}