In this particular demo, we’re going to use some system calls
Now first, check the open
manual by entering:
man 2 open
If we just type man open
it will open a manual for a normal user. So use man 2 open
to access the programmer’s manual
Check linux system calls here:
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main(){
char msg[50] = "Halo semuanya";
char buffer[50];
int fd = open("samples2.txt",O_RDWR);
printf("fd = %d\\n", fd);
if (fd != -1) {
printf("File opened successfully\\n");
write(fd, msg, sizeof(msg));
lseek(fd,0,SEEK_SET);
read(fd, buffer, sizeof(buffer));
printf("Isi Textnya Adalah: %s\\n", buffer);
close(fd);
}
else {
printf("File not found\\nCreating new file\\n");
fd = open("samples2.txt", O_RDWR | O_CREAT, 0777);
printf("fd = %d\\n", fd);
printf("File created successfully\\n");
write(fd, msg, sizeof(msg));
lseek(fd,0,SEEK_SET);
read(fd, buffer, sizeof(buffer));
printf("Isi Textnya Adalah: %s\\n", buffer);
close(fd);
}
return 0;
}