SCR_public/23SCR/SCR011/get_numb.c
2024-12-09 11:58:49 +01:00

49 lines
792 B
C

#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#define szbuf 256
int main(int argc, char const *argv[])
{
char buf[szbuf];
int fd, l, n, x;
long int i, offset;
if(argc<3)
{
fprintf(stderr, "Usage: %s ./get_numb <file_name> <offset>",argv[1]);
exit(1);
}
fd = open(argv[1],O_RDONLY);
if(fd == -1)
{
perror("Opening file fails");
exit(2);
}
i = strtol(argv[2],NULL,0);
offset = i* sizeof(int);
l = lseek(fd,offset,SEEK_SET);
if(l == -1)
{
perror("lseek fails");
exit(3);
}
n = read(fd,&x,sizeof(int));
if(n ==-1)
{
perror("road in file fails");
exit(4);
}
if(n == 0)
{
printf("Offset is out of range!\n");
}
else
{
printf("The number at offset %d is 0x%08e --> %d\n",i,x,x);
}
close(fd);
exit(0);
}