-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.c
78 lines (57 loc) · 1.26 KB
/
read.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
76
77
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <sys/stat.h>
pthread_mutex_t mutexWrArray;
struct thread_data
{
int first_index;
int last_index;
int cur_index;
int *arr;
int *new_arr;
int *global_count;
int count;
};
int ReadFromFile(char *input_file, char **arr_pointer)
{
FILE *input;
if((input=fopen(input_file,"rb"))==0)
{
printf("Can't open input file.\n");
exit(-1);
}
struct stat file_stat;
stat(input_file, &file_stat);
int count = file_stat.st_size/sizeof(char);
//printf("count = %d\n\n", count);
int i = 0;
char *arr = malloc(count * sizeof(char));
while (fread(&arr[i++], sizeof(char), count, input)){}
*arr_pointer = arr;
fclose(input);
return count;
}
void WriteToFile(char *output_file, char *arr, int count)
{
FILE *output;
int i;
if((output=fopen(output_file,"w"))==0)
{
puts ("Can't open output file.");
exit(-1);
}
for (i=0; i<count; i++)
{
fprintf(output, "\\x%x\n", arr[i]);
}
fclose(output);
}
int main(int argc, char *argv[])
{
char *arr = NULL;
int count = ReadFromFile(argv[1], &arr);
WriteToFile(argv[2], arr, count);
free(arr);
return 0;
}