fcntl
Fri Jun 07 2024 04:49:24 GMT+0000 (Coordinated Universal Time)
Saved by
@prabhas
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
int file;
int flags;
// Open file
file = open("file.txt", O_RDWR);
if (file == -1) {
perror("Error opening file");
exit(EXIT_FAILURE);
}
// Get file descriptor flags
flags = fcntl(file, F_GETFL);
if (flags == -1) {
perror("Error getting file flags");
close(file);
exit(EXIT_FAILURE);
}
// Set file descriptor flags to append mode
flags |= O_APPEND;
if (fcntl(file, F_SETFL, flags) == -1) {
perror("Error setting file flags");
close(file);
exit(EXIT_FAILURE);
}
// Write to file
if (write(file, "Appending this line\n", 20) == -1) {
perror("Error writing to file");
}
// Close file
close(file);
return 0;
}
content_copyCOPY
Comments