Skip to content

Instantly share code, notes, and snippets.

@cho0h5
Last active December 18, 2021 04:02
Show Gist options
  • Save cho0h5/a333e71c0c0d7e08858925fe25a7b7ee to your computer and use it in GitHub Desktop.
Save cho0h5/a333e71c0c0d7e08858925fe25a7b7ee to your computer and use it in GitHub Desktop.
c file io
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd = open("foo.txt", O_RDWR | O_CREAT, 0644);
if(fd < 0) {
printf("failed\n");
return -1;
}
printf("success\n");
printf("fd: %d\n", fd);
char buf[10];
read(fd, buf, 10);
close(fd);
printf("file content: %s", buf);
return 0;
}
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd = open("foo.txt", O_RDWR | O_CREAT, 0644);
if(fd < 0) {
printf("failed\n");
return -1;
}
printf("success\n");
printf("fd: %d\n", fd);
write(fd, "hello", 5);
close(fd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment