lseek
Fri Jun 07 2024 04:52:04 GMT+0000 (Coordinated Universal Time)
Saved by
@prabhas
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
int file;
off_t offset;
char buffer[20];
// Open file
file = open("file.txt", O_RDONLY);
if (file == -1) {
perror("Error opening file");
exit(EXIT_FAILURE);
}
// Move file offset to 10 bytes from the beginning
offset = lseek(file, 10, SEEK_SET);
if (offset == -1) {
perror("Error seeking in file");
close(file);
exit(EXIT_FAILURE);
}
// Read from the new offset
if (read(file, buffer, 20) == -1) {
perror("Error reading from file");
close(file);
exit(EXIT_FAILURE);
}
// Print the read content
printf("Read content: %.*s\n", 20, buffer);
// Close file
close(file);
return 0;
}
content_copyCOPY
Comments