#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;
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter