File tree Expand file tree Collapse file tree 3 files changed +31
-43
lines changed Expand file tree Collapse file tree 3 files changed +31
-43
lines changed Original file line number Diff line number Diff line change
1
+ #include "helpers.h"
1
2
#include <ctype.h>
2
3
#include <stdbool.h>
3
4
#include <stdio.h>
@@ -156,24 +157,6 @@ static int parse_with_rules(const char *message) {
156
157
return count ;
157
158
}
158
159
159
- static char * read_file (const char * file_path ) {
160
- FILE * file = fopen (file_path , "r" );
161
- if (!file ) {
162
- perror ("Unable to open file" );
163
- exit (EXIT_FAILURE );
164
- }
165
-
166
- fseek (file , 0 , SEEK_END );
167
- long file_size = ftell (file );
168
- rewind (file );
169
-
170
- char * content = malloc (file_size + 1 );
171
- fread (content , 1 , file_size , file );
172
- content [file_size ] = '\0' ;
173
- fclose (file );
174
- return content ;
175
- }
176
-
177
160
int main () {
178
161
char * message = read_file ("crates/day03/input.txt" );
179
162
Original file line number Diff line number Diff line change
1
+ #include "helpers.h"
1
2
#include <stdbool.h>
2
3
#include <stdio.h>
3
4
#include <stdlib.h>
@@ -209,31 +210,6 @@ static int check_and_sort_page_order(const struct PrintQueueChecker *checker) {
209
210
return sum ;
210
211
}
211
212
212
- static char * read_file (const char * filename ) {
213
- FILE * file = fopen (filename , "r" );
214
- if (file == NULL ) {
215
- fprintf (stderr , "Unable to open file: %s\n" , filename );
216
- exit (1 );
217
- }
218
-
219
- fseek (file , 0 , SEEK_END );
220
- long file_size = ftell (file );
221
- rewind (file );
222
-
223
- char * buffer = (char * )malloc (file_size + 1 );
224
- if (buffer == NULL ) {
225
- fclose (file );
226
- fprintf (stderr , "Memory allocation failed\n" );
227
- exit (1 );
228
- }
229
-
230
- size_t read_size = fread (buffer , 1 , file_size , file );
231
- buffer [read_size ] = '\0' ;
232
-
233
- fclose (file );
234
- return buffer ;
235
- }
236
-
237
213
static void free_print_queue_checker (struct PrintQueueChecker * checker ) {
238
214
for (int i = 0 ; i < checker -> rule_count ; i ++ ) {
239
215
free (checker -> rules [i ].values );
Original file line number Diff line number Diff line change
1
+ #include <stdbool.h>
2
+ #include <stdio.h>
3
+ #include <stdlib.h>
4
+ #include <string.h>
5
+
6
+ static char * read_file (const char * filename ) {
7
+ FILE * file = fopen (filename , "r" );
8
+ if (file == NULL ) {
9
+ fprintf (stderr , "Unable to open file: %s\n" , filename );
10
+ exit (1 );
11
+ }
12
+
13
+ fseek (file , 0 , SEEK_END );
14
+ long file_size = ftell (file );
15
+ rewind (file );
16
+
17
+ char * buffer = (char * )malloc (file_size + 1 );
18
+ if (buffer == NULL ) {
19
+ fclose (file );
20
+ fprintf (stderr , "Memory allocation failed\n" );
21
+ exit (1 );
22
+ }
23
+
24
+ size_t read_size = fread (buffer , 1 , file_size , file );
25
+ buffer [read_size ] = '\0' ;
26
+
27
+ fclose (file );
28
+ return buffer ;
29
+ }
You can’t perform that action at this time.
0 commit comments