-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.c
31 lines (28 loc) · 859 Bytes
/
io.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
#include <stdio.h>
#include <stdlib.h>
#define LCP_EOF '\t'
// linux vs. windows PROBLEM!!!
void io_str_input(char* filename, char** dst, int* len) {
FILE* file = fopen(filename, "r");
fseek(file, 0, SEEK_END);
*len = ftell(file); // in linux " - 1" should be removed because windows makes new line in file with 2 character: \r\n
fseek(file, 0, SEEK_SET);
*dst = (char*)malloc((*len + 1) * sizeof(char));
fscanf(file, "%s", *dst);
(*dst)[*len - 1] = LCP_EOF;
(*dst)[*len] = 0;
fclose(file);
}
void io_int_output(char* filename, int* data, int len) {
FILE* file = fopen(filename, "w");
int i = 0;
for (i = 1; i <= len + 1; i++) {
fprintf(file, "%d\n", data[i]);
}
fclose(file);
}
void io_str_output(char* filename, char* data, int len) {
FILE* file = fopen(filename, "w");
fprintf(file, "%s\n", data);
fclose(file);
}