Files and File Descriptors
File System Calls and File Descriptors
Consider this code:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main() {
int fd1 = open("myfile.txt", O_WRONLY|O_CREAT|O_TRUNC, 0644);
int fd2 = dup(fd1);
int fd3 = open("myfile.txt", O_RDONLY);
int fd4 = open("myfile.txt", O_RDONLY);
char buf[6] = {0}; // Initialize buffer to all zeros
write(fd1, "Hello", 5);
write(fd2, "World", 5);
read(fd3, buf, 5);
printf("Read from fd3: %s\n", buf);
read(fd4, buf, 5);
printf("Read from fd4: %s\n", buf);
return 0;
}
(When logged in, completion status appears here.)