Os pgm-2 - r22
Os pgm-2 - r22
Write programs using the I/O system calls of UNIX/LINUX operating system
(open, read, write, close, fcntl, seek, stat, opendir, readdir).
Open:
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
int main() {
// Open the file for writing (create if doesn't exist)
int fd = open("example.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd == -1) {
perror("open error");
return 1;
}
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
int main() {
// Open the file for reading
int fd = open("example.txt", O_RDONLY);
if (fd == -1) {
perror("open");
return 1;
}
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
int main() {
// Open the file for reading
int fd = open("example.txt", O_RDONLY);
if (fd == -1) {
perror("open");
return 1;
}
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
int main() {
// Open the file for reading and writing
int fd = open("example.txt", O_RDWR);
if (fd == -1) {
perror("open");
return 1;
}
#include <sys/stat.h>
#include <stdio.h>
int main() {
struct stat file_info;
// Get information about the file
if (stat("example.txt", &file_info) == -1) {
perror("stat");
return 1;
}
return 0;
}
#include <dirent.h>
#include <stdio.h>
int main() {
DIR *dir = opendir(".");
if (dir == NULL) {
perror("opendir");
return 1;
}