SCR/SCR1.2/TP11/get_numb.c
2023-03-03 21:52:30 +01:00

35 lines
707 B
C

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char *argv[]){
if (argc != 3){
fprintf(stderr, "%c <file_name> <offset>", argv[0]);
exit(1);
}
off_t offset;
ssize_t nb_read;
int nb, n;
int fdin;
fdin = open(argv[1], O_RDONLY);
if (fdin == -1){
perror("File can't be opened");
exit(2);
}
offset = (off_t) strtol (argv[2], NULL, 0);
n = lseek(fdin, offset * sizeof(int), SEEK_SET);
if (!n){
perror("Offset is out of range!");
exit(3);
}
nb_read=read(fdin, &nb, sizeof(int));
if (nb_read < 0){
perror("File can't be read");
exit(4);
}
printf("The number at offset %s is %x --> %d\n", argv[2], nb, nb);
return EXIT_SUCCESS;
}