编程实现一个简单的文件系统,要求实现以下功能: 文件创建 文件解密 文件显示 文件列目录 文件删除 文件加密 文件合并 文件查询 文件复制 文件重命名,c语言实现
时间: 2024-05-04 08:20:26 浏览: 121
以下是一个简单的文件系统的C语言实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_FILENAME_LENGTH 50
#define MAX_FILE_COUNT 100
struct File {
char name[MAX_FILENAME_LENGTH];
int size;
char *data;
int is_encrypted;
};
struct FileSystem {
struct File files[MAX_FILE_COUNT];
int file_count;
};
void create_file(struct FileSystem *fs, char *name, int size) {
if (fs->file_count == MAX_FILE_COUNT) {
printf("Error: maximum number of files reached\n");
return;
}
struct File *file = &fs->files[fs->file_count];
strncpy(file->name, name, MAX_FILENAME_LENGTH);
file->size = size;
file->data = malloc(size);
file->is_encrypted = 0;
fs->file_count++;
printf("File created: %s\n", name);
}
struct File *find_file(struct FileSystem *fs, char *name) {
for (int i = 0; i < fs->file_count; i++) {
if (strcmp(fs->files[i].name, name) == 0) {
return &fs->files[i];
}
}
return NULL;
}
void delete_file(struct FileSystem *fs, char *name) {
struct File *file = find_file(fs, name);
if (file == NULL) {
printf("Error: file not found\n");
return;
}
free(file->data);
memmove(file, file + 1, (fs->file_count - 1 - (file - fs->files)) * sizeof(struct File));
fs->file_count--;
printf("File deleted: %s\n", name);
}
void list_files(struct FileSystem *fs) {
printf("Files:\n");
for (int i = 0; i < fs->file_count; i++) {
printf("%s (%d bytes)\n", fs->files[i].name, fs->files[i].size);
}
}
void rename_file(struct FileSystem *fs, char *old_name, char *new_name) {
struct File *file = find_file(fs, old_name);
if (file == NULL) {
printf("Error: file not found\n");
return;
}
strncpy(file->name, new_name, MAX_FILENAME_LENGTH);
printf("File renamed: %s -> %s\n", old_name, new_name);
}
void copy_file(struct FileSystem *fs, char *source_name, char *dest_name) {
struct File *source_file = find_file(fs, source_name);
if (source_file == NULL) {
printf("Error: source file not found\n");
return;
}
if (find_file(fs, dest_name) != NULL) {
printf("Error: destination file already exists\n");
return;
}
struct File *dest_file = &fs->files[fs->file_count];
strncpy(dest_file->name, dest_name, MAX_FILENAME_LENGTH);
dest_file->size = source_file->size;
dest_file->data = malloc(dest_file->size);
memcpy(dest_file->data, source_file->data, dest_file->size);
dest_file->is_encrypted = source_file->is_encrypted;
fs->file_count++;
printf("File copied: %s -> %s\n", source_name, dest_name);
}
void encrypt_file(struct FileSystem *fs, char *name) {
struct File *file = find_file(fs, name);
if (file == NULL) {
printf("Error: file not found\n");
return;
}
if (file->is_encrypted) {
printf("Error: file is already encrypted\n");
return;
}
for (int i = 0; i < file->size; i++) {
file->data[i] ^= 0xff;
}
file->is_encrypted = 1;
printf("File encrypted: %s\n", name);
}
void decrypt_file(struct FileSystem *fs, char *name) {
struct File *file = find_file(fs, name);
if (file == NULL) {
printf("Error: file not found\n");
return;
}
if (!file->is_encrypted) {
printf("Error: file is not encrypted\n");
return;
}
for (int i = 0; i < file->size; i++) {
file->data[i] ^= 0xff;
}
file->is_encrypted = 0;
printf("File decrypted: %s\n", name);
}
void merge_files(struct FileSystem *fs, char *name1, char *name2, char *new_name) {
struct File *file1 = find_file(fs, name1);
if (file1 == NULL) {
printf("Error: file 1 not found\n");
return;
}
struct File *file2 = find_file(fs, name2);
if (file2 == NULL) {
printf("Error: file 2 not found\n");
return;
}
if (find_file(fs, new_name) != NULL) {
printf("Error: destination file already exists\n");
return;
}
struct File *dest_file = &fs->files[fs->file_count];
strncpy(dest_file->name, new_name, MAX_FILENAME_LENGTH);
dest_file->size = file1->size + file2->size;
dest_file->data = malloc(dest_file->size);
memcpy(dest_file->data, file1->data, file1->size);
memcpy(dest_file->data + file1->size, file2->data, file2->size);
dest_file->is_encrypted = file1->is_encrypted || file2->is_encrypted;
fs->file_count++;
printf("Files merged: %s + %s -> %s\n", name1, name2, new_name);
}
void query_file(struct FileSystem *fs, char *name) {
struct File *file = find_file(fs, name);
if (file == NULL) {
printf("Error: file not found\n");
return;
}
printf("File %s:\n", name);
printf("Size: %d bytes\n", file->size);
if (file->is_encrypted) {
printf("Encrypted: yes\n");
} else {
printf("Encrypted: no\n");
}
}
int main() {
struct FileSystem fs = {0};
while (1) {
char command[100];
printf("> ");
if (fgets(command, sizeof(command), stdin) == NULL) {
break;
}
if (strcmp(command, "exit\n") == 0) {
break;
}
char *args[10];
int arg_count = 0;
char *token = strtok(command, " \n");
while (token != NULL) {
args[arg_count++] = token;
token = strtok(NULL, " \n");
}
if (strcmp(args[0], "create") == 0) {
if (arg_count != 3) {
printf("Usage: create <name> <size>\n");
} else {
create_file(&fs, args[1], atoi(args[2]));
}
} else if (strcmp(args[0], "delete") == 0) {
if (arg_count != 2) {
printf("Usage: delete <name>\n");
} else {
delete_file(&fs, args[1]);
}
} else if (strcmp(args[0], "list") == 0) {
if (arg_count != 1) {
printf("Usage: list\n");
} else {
list_files(&fs);
}
} else if (strcmp(args[0], "rename") == 0) {
if (arg_count != 3) {
printf("Usage: rename <old_name> <new_name>\n");
} else {
rename_file(&fs, args[1], args[2]);
}
} else if (strcmp(args[0], "copy") == 0) {
if (arg_count != 3) {
printf("Usage: copy <source_name> <dest_name>\n");
} else {
copy_file(&fs, args[1], args[2]);
}
} else if (strcmp(args[0], "encrypt") == 0) {
if (arg_count != 2) {
printf("Usage: encrypt <name>\n");
} else {
encrypt_file(&fs, args[1]);
}
} else if (strcmp(args[0], "decrypt") == 0) {
if (arg_count != 2) {
printf("Usage: decrypt <name>\n");
} else {
decrypt_file(&fs, args[1]);
}
} else if (strcmp(args[0], "merge") == 0) {
if (arg_count != 4) {
printf("Usage: merge <name1> <name2> <new_name>\n");
} else {
merge_files(&fs, args[1], args[2], args[3]);
}
} else if (strcmp(args[0], "query") == 0) {
if (arg_count != 2) {
printf("Usage: query <name>\n");
} else {
query_file(&fs, args[1]);
}
} else {
printf("Unknown command: %s", command);
}
}
for (int i = 0; i < fs.file_count; i++) {
free(fs.files[i].data);
}
return 0;
}
```
该文件系统支持创建、删除、列出文件、重命名、复制、加密、解密、合并、查询文件等功能。在命令行中输入命令来使用该文件系统。例如:
```
> create file1 100
File created: file1
> list
Files:
file1 (100 bytes)
> copy file1 file2
File copied: file1 -> file2
> merge file1 file2 file3
Files merged: file1 + file2 -> file3
> query file1
File file1:
Size: 100 bytes
Encrypted: no
> encrypt file1
File encrypted: file1
> query file1
File file1:
Size: 100 bytes
Encrypted: yes
> exit
```
阅读全文