Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
749 B
3
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>

int main() {
    int fd;
    char buffer[4];
    pid_t pid;
    char filename[] = "data.txt";

    pid = fork();

    if (pid < 0) {
        perror("Failed to fork");
        return 1;
    }

    if (pid == 0) {
        fd = open(filename, O_RDONLY);
        if (fd < 0) {
            perror("Failed to open file in child");
            return 1;
        }
        lseek(fd, 3, SEEK_SET);
        read(fd, buffer, 4);
        printf("Buffer contents: %d, %d, %d, %d\n", buffer[0], buffer[1], buffer[2], buffer[3]);
        close(fd);
    } else {
        wait(NULL); 
    }

    return 0;
}
Leave a Comment