Class Activity - USP - 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Simple File Manager

This example illustrates how to use UNIX file-related APIs for basic file
operations.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>

void create_file(const char *filename) {


int fd = open(filename, O_CREAT | O_WRONLY, 0644);
if (fd == -1) {
perror("Error creating file");
return;
}
close(fd);
printf("File '%s' created.\n", filename);
}

void list_files() {
DIR *dir = opendir(".");
struct dirent *entry;

if (dir == NULL) {
perror("Error opening directory");
return;
}

printf("Files in current directory:\n");


while ((entry = readdir(dir)) != NULL) {
printf("%s\n", entry->d_name);
}
closedir(dir);
}

void delete_file(const char *filename) {


if (unlink(filename) == -1) {
perror("Error deleting file");
return;
}
printf("File '%s' deleted.\n", filename);
}

int main(int argc, char *argv[]) {


if (argc < 2) {
fprintf(stderr, "Usage: %s <create|list|delete> [filename]\n", argv[0]);
return 1;
}

if (strcmp(argv[1], "create") == 0 && argc == 3) {


create_file(argv[2]);
} else if (strcmp(argv[1], "list") == 0) {
list_files();
} else if (strcmp(argv[1], "delete") == 0 && argc == 3) {
delete_file(argv[2]);
} else {
fprintf(stderr, "Invalid command or missing filename.\n");
}

return 0;
}

How to Compile and Run


1. Save the code to a file, e.g., file_manager.c.
2. Open a terminal and navigate to the directory containing the file.
3. Compile the code:
gcc -o file_manager file_manager.c

4. Run the program:


To create a file:
./file_manager create test.txt
To list files in the current directory:
./file_manager list
To delete a file:
./file_manager delete test.txt
This program allows users to create a text file, append content to it, read its
content, and delete it.
This simple text file editor demonstrates how to use UNIX file-related APIs for
file creation, reading, appending, and deletion.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

void create_file(const char *filename) {


int fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0644);
if (fd == -1) {
perror("Error creating file");
return;
}
close(fd);
printf("File '%s' created.\n", filename);
}

void append_to_file(const char *filename, const char *text) {


int fd = open(filename, O_WRONLY | O_APPEND);
if (fd == -1) {
perror("Error opening file for appending");
return;
}
write(fd, text, strlen(text));
write(fd, "\n", 1); // Add newline
close(fd);
printf("Appended to '%s'.\n", filename);
}

void read_file(const char *filename) {


char buffer[256];
int fd = open(filename, O_RDONLY);
if (fd == -1) {
perror("Error opening file for reading");
return;
}

printf("Contents of '%s':\n", filename);


ssize_t bytes_read;
while ((bytes_read = read(fd, buffer, sizeof(buffer) - 1)) > 0) {
buffer[bytes_read] = '\0'; // Null-terminate the string
printf("%s", buffer);
}
close(fd);
}

void delete_file(const char *filename) {


if (unlink(filename) == -1) {
perror("Error deleting file");
return;
}
printf("File '%s' deleted.\n", filename);
}

int main(int argc, char *argv[]) {


if (argc < 2) {
fprintf(stderr, "Usage: %s <create|append|read|delete> [filename] [text]\n",
argv[0]);
return 1;
}

if (strcmp(argv[1], "create") == 0 && argc == 3) {


create_file(argv[2]);
} else if (strcmp(argv[1], "append") == 0 && argc == 4) {
append_to_file(argv[2], argv[3]);
} else if (strcmp(argv[1], "read") == 0 && argc == 3) {
read_file(argv[2]);
} else if (strcmp(argv[1], "delete") == 0 && argc == 3) {
delete_file(argv[2]);
} else {
fprintf(stderr, "Invalid command or missing arguments.\n");
}

return 0;
}
How to Compile and Run
1. Save the code to a file, e.g., text_editor.c.
2. Open a terminal and navigate to the directory containing the file.
3. Compile the code:
gcc -o text_editor text_editor.c
4. Run the program:
To create a file:
./text_editor create myfile.txt
To append text to the file:
./text_editor append myfile.txt "Hello, world!"
To read the contents of the file:
./text_editor read myfile.txt
To delete the file:
./text_editor delete myfile.txt

You might also like