-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
51 lines (41 loc) · 1.14 KB
/
main.cpp
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
#include <stdint.h>
#include <stdio.h>
#include <netinet/in.h>
#include <errno.h>
#include <string.h>
uint32_t add_nbo(const char* filename, int* error) {
FILE *fp = fopen(filename, "rb");
if (fp == NULL) {
*error = errno;
return 0;
}
uint32_t n;
size_t read_size = fread(&n, 1, sizeof(n), fp);
fclose(fp);
if (read_size < sizeof(n)) {
*error = EINVAL; // Invalid argument (file too small)
return 0;
}
*error = 0;
return ntohl(n);
}
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage: %s <file1> <file2>\n", argv[0]);
return 1;
}
int error;
uint32_t n1 = add_nbo(argv[1], &error);
if (error != 0) {
fprintf(stderr, "Error reading %s: %s\n", argv[1], strerror(error));
return 1;
}
uint32_t n2 = add_nbo(argv[2], &error);
if (error != 0) {
fprintf(stderr, "Error reading %s: %s\n", argv[2], strerror(error));
return 1;
}
uint32_t sum = n1 + n2; // Overflow is ignored as requested
printf("%u(0x%x) + %u(0x%x) = %u(0x%x)\n", n1, n1, n2, n2, sum, sum);
return 0;
}