
problema con codigo
Publicado por cristian (1 intervención) el 18/09/2023 03:41:35
hola gente estoy tratando de crear un codigo que lea un archivo.bin y imprima informacion selectiva pero el archivo esta estructurado de una forma en la que me confunde no se si me puedan guiar anexo el codigo y las imagenes de que me arroja la consola al momento de ejecutar sla consola







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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
struct Student {
uint32_t student_id;
int gender;
char student_name;
int age;
};
int main(int argc, char *argv[]) {
if (argc != 4) {
fprintf(stderr, "Usage: %s <binary file> <min age> <max age>\n", argv[0]);
return EXIT_FAILURE;
}
int min_age = atoi(argv[2]);
int max_age = atoi(argv[3]);
if (min_age > max_age) {
fprintf(stderr, "Error: la edad minima no puede ser mayor a la maxima\n");
return EXIT_FAILURE;
}
FILE *fp = fopen(argv[1], "rb");
if (fp == NULL) {
perror("Error opening file");
return EXIT_FAILURE;
}
int num_students;
fread(&num_students, sizeof(int), 1, fp);
printf("Nombre\t\t\tEdad\tGenero\n");
printf("------------------------------\n");
for (int i = 0; i < num_students; i++) {
char student_name[24];
int age;
char gender;
fread(student_name, sizeof(char), 24, fp);
fread(&age, sizeof(int), 1, fp);
fread(&gender, sizeof(char), 1, fp);
if (age > min_age && age < max_age) {
char converted_gender = (gender & 1) ? 'F' : 'M';
printf("%s\t\t\t %d\t %c\n", student_name, age, converted_gender);
}
}
fclose(fp);
return EXIT_SUCCESS;
}







Valora esta pregunta


0