Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <sys/stat.h>
- void createFile(const char *filename) {
- int fd = open(filename, O_CREAT | O_WRONLY, 0644);
- if (fd == -1) {
- perror("Error creating file");
- return;
- }
- printf("File '%s' created successfully!\n", filename);
- close(fd);
- }
- void deleteFile(const char *filename) {
- if (unlink(filename) == 0) {
- printf("File '%s' deleted successfully!\n", filename);
- } else {
- perror("Error deleting file");
- }
- }
- void renameFile(const char *oldname, const char *newname) {
- if (rename(oldname, newname) == 0) {
- printf("File renamed from '%s' to '%s' successfully!\n", oldname, newname);
- } else {
- perror("Error renaming file");
- }
- }
- void changePermissions(const char *filename, mode_t mode) {
- if (chmod(filename, mode) == 0) {
- printf("Permissions of '%s' changed successfully!\n", filename);
- } else {
- perror("Error changing file permissions");
- }
- }
- int main() {
- int choice;
- char filename[100], newname[100];
- mode_t mode;
- while (1) {
- printf("\nFile Operations Menu:\n");
- printf("1. Create File\n");
- printf("2. Delete File\n");
- printf("3. Rename File\n");
- printf("4. Change File Permissions\n");
- printf("5. Exit\n");
- printf("Enter your choice: ");
- scanf("%d", &choice);
- switch (choice) {
- case 1:
- printf("Enter filename to create: ");
- scanf("%s", filename);
- createFile(filename);
- break;
- case 2:
- printf("Enter filename to delete: ");
- scanf("%s", filename);
- deleteFile(filename);
- break;
- case 3:
- printf("Enter current filename: ");
- scanf("%s", filename);
- printf("Enter new filename: ");
- scanf("%s", newname);
- renameFile(filename, newname);
- break;
- case 4:
- printf("Enter filename to change permissions: ");
- scanf("%s", filename);
- printf("Enter new permissions (octal, e.g., 0644): ");
- scanf("%o", &mode);
- changePermissions(filename, mode);
- break;
- case 5:
- printf("Exiting...\n");
- exit(0);
- default:
- printf("Invalid choice! Please try again.\n");
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement