用C语言写一个简单的文件系统,代码要求:具备基本的文件处理功能,包括实现文件的建立、打开、删除、关闭、复制、读、写、查询等功能。
时间: 2023-10-20 17:14:03 浏览: 207
以下是一个简单的基于C语言的文件系统的实现,具备基本的文件处理功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_FILENAME_LENGTH 100
#define MAX_FILE_SIZE 1000
typedef struct {
char filename[MAX_FILENAME_LENGTH];
int size;
char* content;
} File;
File* files[10];
int num_files = 0;
void create_file(char* filename) {
if (num_files == 10) {
printf("Error: Maximum number of files reached.\n");
return;
}
for (int i = 0; i < num_files; i++) {
if (strcmp(files[i]->filename, filename) == 0) {
printf("Error: File with same name already exists.\n");
return;
}
}
File* new_file = (File*) malloc(sizeof(File));
strcpy(new_file->filename, filename);
new_file->size = 0;
new_file->content = (char*) malloc(sizeof(char) * MAX_FILE_SIZE);
files[num_files] = new_file;
num_files++;
printf("File created successfully.\n");
}
File* find_file(char* filename) {
for (int i = 0; i < num_files; i++) {
if (strcmp(files[i]->filename, filename) == 0) {
return files[i];
}
}
return NULL;
}
void open_file(char* filename) {
File* file = find_file(filename);
if (file == NULL) {
printf("Error: File not found.\n");
return;
}
printf("File opened successfully.\n");
}
void close_file(char* filename) {
File* file = find_file(filename);
if (file == NULL) {
printf("Error: File not found.\n");
return;
}
printf("File closed successfully.\n");
}
void delete_file(char* filename) {
File* file = find_file(filename);
if (file == NULL) {
printf("Error: File not found.\n");
return;
}
for (int i = 0; i < num_files; i++) {
if (files[i] == file) {
for (int j = i; j < num_files-1; j++) {
files[j] = files[j+1];
}
num_files--;
free(file->content);
free(file);
printf("File deleted successfully.\n");
return;
}
}
}
void copy_file(char* filename1, char* filename2) {
File* file1 = find_file(filename1);
if (file1 == NULL) {
printf("Error: File 1 not found.\n");
return;
}
File* file2 = find_file(filename2);
if (file2 != NULL) {
printf("Error: File 2 already exists.\n");
return;
}
File* new_file = (File*) malloc(sizeof(File));
strcpy(new_file->filename, filename2);
new_file->size = file1->size;
new_file->content = (char*) malloc(sizeof(char) * new_file->size);
memcpy(new_file->content, file1->content, new_file->size);
files[num_files] = new_file;
num_files++;
printf("File copied successfully.\n");
}
void read_file(char* filename) {
File* file = find_file(filename);
if (file == NULL) {
printf("Error: File not found.\n");
return;
}
printf("File contents:\n%s\n", file->content);
}
void write_file(char* filename, char* content) {
File* file = find_file(filename);
if (file == NULL) {
printf("Error: File not found.\n");
return;
}
if (strlen(content) > MAX_FILE_SIZE) {
printf("Error: File size too large.\n");
return;
}
file->size = strlen(content);
strcpy(file->content, content);
printf("File written successfully.\n");
}
void list_files() {
printf("List of files:\n");
for (int i = 0; i < num_files; i++) {
printf("%s\n", files[i]->filename);
}
}
int main() {
char command[20], filename1[MAX_FILENAME_LENGTH], filename2[MAX_FILENAME_LENGTH], content[MAX_FILE_SIZE];
while (1) {
printf("> ");
scanf("%s", command);
if (strcmp(command, "create") == 0) {
scanf("%s", filename1);
create_file(filename1);
} else if (strcmp(command, "open") == 0) {
scanf("%s", filename1);
open_file(filename1);
} else if (strcmp(command, "close") == 0) {
scanf("%s", filename1);
close_file(filename1);
} else if (strcmp(command, "delete") == 0) {
scanf("%s", filename1);
delete_file(filename1);
} else if (strcmp(command, "copy") == 0) {
scanf("%s", filename1);
scanf("%s", filename2);
copy_file(filename1, filename2);
} else if (strcmp(command, "read") == 0) {
scanf("%s", filename1);
read_file(filename1);
} else if (strcmp(command, "write") == 0) {
scanf("%s", filename1);
scanf("%s", content);
write_file(filename1, content);
} else if (strcmp(command, "list") == 0) {
list_files();
} else if (strcmp(command, "exit") == 0) {
break;
} else {
printf("Invalid command.\n");
}
}
return 0;
}
```
该文件系统使用一个File结构体来表示文件,其中包含文件名、大小和内容的指针。文件系统最多支持10个文件,文件内容最大为1000个字符。文件系统支持创建、打开、关闭、删除、复制、读、写和查询操作。用户可以通过命令行输入来执行这些操作。
阅读全文