-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_binary.c
More file actions
62 lines (54 loc) · 2.86 KB
/
Copy pathsort_binary.c
File metadata and controls
62 lines (54 loc) · 2.86 KB
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
#include <stdio.h>
#include <stdlib.h>
int compare_ints(const void *a, const void *b) {
const int *ia = (const int *)a;
const int *ib = (const int *)b;
return *ia - *ib;
}
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage %s input.bin output.bin\n", argv[0]);
return 1;
}
FILE *input_file = fopen(argv[1], "rb");
if (!input_file) {
perror("Error opening input file");
return 1;
}
fseek(input_file, 0, SEEK_END); // moves the file position to the end
long file_size = ftell(input_file); //returns the current file position of the file pointer. since it was just moved to the end ftell returns the total size of the file
rewind(input_file); //resets the file position indicator to the beginning of the file
if (file_size % sizeof(int) != 0) { // checks if the size of the file is a multiple of the size of an integer. This is important because the code likely intends to process the file contents as integers. If the file size isn't an exact multiple of the size of an integer, this indicates that the file may not contain a proper sequence of integers.
fprintf(stderr, "Invalid binary file format.\n");
fclose(input_file);
return 1;
}
int num_elements = file_size / sizeof(int);
int *numbers = malloc(file_size); //This line allocates enough space in memory to store all bytes of the file.
if (!numbers) {
fprintf(stderr, "Memory allocation failed.\n");
fclose(input_file);
return 1;
}
//Read integers from binary file
fread(numbers, sizeof(int), num_elements, input_file);
// numbers: This is the pointer to the buffer where the read data will be stored. In this case, numbers points to a block of memory that has been previously allocated to hold the integers read from the file.
// sizeof(int): This specifies the size of each element to be read. Since the file contains integers
// num_elements: This is the number of elements to read. You've calculated this earlier by dividing the file size by the size of an integer. This tells fread how many integers it should read from the file.
// input_file: This is the file pointer from which the data is to be read. The file pointer should be correctly positioned at the start of the data to be read (or wherever necessary), which you've ensured by using rewind earlier in the code.
fclose(input_file);
//sort the numbers
qsort(numbers, num_elements, sizeof(int), compare_ints);
FILE *output_file = fopen(argv[2], "wb");
if (!output_file) {
perror("Error opening file");
free(numbers);
return 1;
}
//write sorted integers to file
fwrite(numbers, sizeof(int), num_elements, output_file);
fclose(output_file);
free(numbers);
printf("Sorting complete, sorted data written to %s\n", argv[2]);
return 0;
}