APL/APL1.2/SCR1.2/TP11/get_numb.c
2022-02-07 10:53:37 +01:00

40 lines
771 B
C

#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>
int main(int argc, char* argv[]) {
int f ,l ,x ,n;
long int i, offset;
if (argc < 3) {
fprintf (stdin,"Usage : %s <file><offset> \n",argv[0]);
exit(1);
}
f=open(argv[1],O_RDONLY);
if (f==1){
perror("Opening file failed !");
exit(2);
}
i = strtol (argv[2],NULL,0);
offset = i*sizeof(int);
l = lseek(f,offset,SEEK_SET);
if (l==-1){
perror("lseek fails !");
exit(3);
}
n = read(f,&x,sizeof(int));
if(n==-1){
perror("reading file fails !");
exit(4);
}
if (n==0){
printf("Out of range !\n");
}else{
printf("The number at offset %d is 0x%08x --> %d \n",i,x,x);
}
close(f);
exit(0);
}