-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_files.c
151 lines (122 loc) · 2.98 KB
/
process_files.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "process_files.h"
/*
Get number of files and file names from input file
file_name: input file name
*/
int get_number_of_files(const char *file_name)
{
int value = 0;
FILE *test_file;
test_file = fopen(file_name,"r");
char first_line[100];
if (!test_file)
{
#ifdef __DEBUG__
int errnum = errno;
printf("get_number_of_files(): File not found %s, error %d, %s!\n", file_name, errnum, strerror( errnum ));
perror("Error printed by perror");
#endif
return -1;
}
int count = 0;
// Reading test file for only 1st line and getting no of file links and saving file links in array
while((fgets(first_line, 100, test_file)) && count < 1)
{
if(count < 1)
{
value = atoi(first_line);
}
count++;
}
fclose(test_file);
return value;
}
int read_numbers_from_file(char* file_name, int* nums, int size)
{
FILE *input_file = NULL;
char line[100];
input_file = fopen(file_name, "r");
if (!input_file)
{
return -1;
}
// Reading from line by line from file and storing links in an int array
int count = 0, start = 0;
while (fgets(line, 100, input_file) != NULL && count <= size)
{
if (line[strlen(line)] == '\n')
{
line[strlen(line)] = '\0';
}
if (line[strlen(line) - 1] == '\n')
{
line[strlen(line) - 1] = '\0';
}
if(start == 0)
{
start++;
}
else
{
nums[count] = atoi(line);
count++;
}
}
fclose(input_file);
return count;
}
int write_numbers_to_file(char* file_name, int num)
{
FILE *output_file = NULL;
output_file = fopen(file_name, "w");
if (!output_file)
{
return -1;
}
fprintf(output_file, "%d", num);
fclose(output_file);
return 0;
}
/*
Read input file and get file links to be processed
params:
file_name:
number_of_files:
file_links:
return:
*/
int read_files(struct app_data_t* app)
{
FILE *test_file = NULL;
test_file = fopen(app->file_name, "r");
if (!test_file)
{
printf("Input file not found...terminating!\n");
return -1;
}
char line[100] = "";
int count = 0, start = 0;
// Reading line by line from file and storing links in an file_links array
while (fgets(line, 100, test_file) != NULL && count < app->number_of_files)
{
if (line[strlen(line)] == '\n')
{
line[strlen(line)] = '\0';
}
if (line[strlen(line) - 1] == '\n')
{
line[strlen(line) - 1] = '\0';
}
if (start == 0)
{
start++;
}
else
{
strcpy(app->file_links[count], line);
count++;
}
}
fclose(test_file);
return ERROR_SUCCESS;
}