-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
64 lines (53 loc) · 1.46 KB
/
main.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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LINE_LENGTH 32000
#define NUM_LINES 100000
#define MIN_LINE 5
int get_output_name (int file_counter, char output_name[64]) {
sprintf(output_name, "c://adata/parts/output%d.json", file_counter);
return (file_counter + 1);
}
int main()
{
FILE * source = NULL, * output = NULL;
char buf[LINE_LENGTH], output_name[64];
int line_counter = 0, file_counter = 0;
source = fopen("c://adata/adata.json", "r");
if (!source) {
printf("ERROR READING FILE\n");
return 1;
}
while (fgets(buf, LINE_LENGTH, source) != NULL) {
if (strlen(buf) > MIN_LINE) {
if ((line_counter % NUM_LINES) == 0) {
file_counter = get_output_name(file_counter, output_name);
printf("Creating file: %s\n", output_name);
output = fopen(output_name, "a+");
fprintf(output, "[\n");
}
if ((line_counter % NUM_LINES) == (NUM_LINES - 1)) {
printf("File completed; %d entries processed.\n\n", (line_counter % NUM_LINES) + 1);
if(buf[strlen(buf) - 2] == ',') {
buf[strlen(buf) - 2] = '\0';
}
fputs(buf, output);
fprintf(output, "\n]");
fclose(output);
output = NULL;
} else {
fputs(buf, output);
}
line_counter ++;
}
}
if (output) {
printf("File completed; %d entries processed.\n\n", (line_counter % NUM_LINES));
fputs(buf, output);
fclose(output);
output = NULL;
}
source = NULL;
fclose(source);
return 0;
}