请写一个c 语言程序,模拟实现一个简单的二级文件管理系统,要求做到以下几点: (1)可以实现常用文件目录和文件操作,如: login 用户登录 dir 列文件目录 create 创建文件 delete 删除文件 open 打开文件 close 关闭文件 read 读文件 write 写文件 (2)列目录时要列出文件名、物理地址、保护码和文件长度 (3)源文件可以进行读写保护
时间: 2024-03-20 15:41:48 浏览: 144
好的,以下是一个简单的二级文件管理系统的 C 语言程序,实现了常用的文件目录和文件操作,并且支持读写保护:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_FILE_NAME_LENGTH 20
#define MAX_FILE_COUNT 100
struct file {
char name[MAX_FILE_NAME_LENGTH];
int address;
int protection;
int length;
int is_open;
char *content;
};
struct directory {
char name[MAX_FILE_NAME_LENGTH];
int file_count;
struct file files[MAX_FILE_COUNT];
};
struct user {
char name[MAX_FILE_NAME_LENGTH];
int is_logged_in;
struct directory directories[2];
};
struct user users[2] = {
{"user1", 0, {{"dir1", 0, {}}, {"dir2", 0, {}}}},
{"user2", 0, {{"dir1", 0, {}}, {"dir2", 0, {}}}}
};
int current_user_index = -1;
int current_directory_index = -1;
void print_error(char *message) {
printf("Error: %s\n", message);
}
void print_success(char *message) {
printf("Success: %s\n", message);
}
void login() {
char username[MAX_FILE_NAME_LENGTH];
printf("Enter username: ");
scanf("%s", username);
int user_found = 0;
for (int i = 0; i < 2; i++) {
if (strcmp(users[i].name, username) == 0) {
current_user_index = i;
user_found = 1;
break;
}
}
if (!user_found) {
print_error("User not found");
return;
}
users[current_user_index].is_logged_in = 1;
printf("Logged in as %s\n", username);
}
void logout() {
if (current_user_index == -1) {
print_error("You are not logged in");
return;
}
users[current_user_index].is_logged_in = 0;
current_user_index = -1;
current_directory_index = -1;
printf("Logged out\n");
}
void select_directory() {
if (current_user_index == -1) {
print_error("You are not logged in");
return;
}
char directory_name[MAX_FILE_NAME_LENGTH];
printf("Enter directory name: ");
scanf("%s", directory_name);
int directory_found = 0;
for (int i = 0; i < 2; i++) {
if (strcmp(users[current_user_index].directories[i].name, directory_name) == 0) {
current_directory_index = i;
directory_found = 1;
break;
}
}
if (!directory_found) {
print_error("Directory not found");
return;
}
printf("Selected directory %s\n", directory_name);
}
void dir() {
if (current_directory_index == -1) {
print_error("No directory selected");
return;
}
printf("%-20s%-20s%-20s%-20s\n", "Name", "Address", "Protection", "Length");
for (int i = 0; i < users[current_user_index].directories[current_directory_index].file_count; i++) {
struct file file = users[current_user_index].directories[current_directory_index].files[i];
printf("%-20s%-20d%-20d%-20d\n", file.name, file.address, file.protection, file.length);
}
}
void create() {
if (current_directory_index == -1) {
print_error("No directory selected");
return;
}
char file_name[MAX_FILE_NAME_LENGTH];
printf("Enter file name: ");
scanf("%s", file_name);
for (int i = 0; i < users[current_user_index].directories[current_directory_index].file_count; i++) {
if (strcmp(users[current_user_index].directories[current_directory_index].files[i].name, file_name) == 0) {
print_error("File already exists");
return;
}
}
struct file new_file;
strcpy(new_file.name, file_name);
new_file.address = rand() % 1000;
new_file.protection = rand() % 2;
new_file.length = 0;
new_file.is_open = 0;
new_file.content = NULL;
users[current_user_index].directories[current_directory_index].files[users[current_user_index].directories[current_directory_index].file_count] = new_file;
users[current_user_index].directories[current_directory_index].file_count++;
print_success("File created");
}
void delete() {
if (current_directory_index == -1) {
print_error("No directory selected");
return;
}
char file_name[MAX_FILE_NAME_LENGTH];
printf("Enter file name: ");
scanf("%s", file_name);
int file_found = 0;
for (int i = 0; i < users[current_user_index].directories[current_directory_index].file_count; i++) {
if (strcmp(users[current_user_index].directories[current_directory_index].files[i].name, file_name) == 0) {
if (users[current_user_index].directories[current_directory_index].files[i].is_open) {
print_error("File is open");
return;
}
free(users[current_user_index].directories[current_directory_index].files[i].content);
for (int j = i; j < users[current_user_index].directories[current_directory_index].file_count - 1; j++) {
users[current_user_index].directories[current_directory_index].files[j] = users[current_user_index].directories[current_directory_index].files[j + 1];
}
users[current_user_index].directories[current_directory_index].file_count--;
file_found = 1;
break;
}
}
if (!file_found) {
print_error("File not found");
return;
}
print_success("File deleted");
}
void open() {
if (current_directory_index == -1) {
print_error("No directory selected");
return;
}
char file_name[MAX_FILE_NAME_LENGTH];
printf("Enter file name: ");
scanf("%s", file_name);
int file_index = -1;
for (int i = 0; i < users[current_user_index].directories[current_directory_index].file_count; i++) {
if (strcmp(users[current_user_index].directories[current_directory_index].files[i].name, file_name) == 0) {
file_index = i;
break;
}
}
if (file_index == -1) {
print_error("File not found");
return;
}
if (users[current_user_index].directories[current_directory_index].files[file_index].is_open) {
print_error("File is already open");
return;
}
users[current_user_index].directories[current_directory_index].files[file_index].is_open = 1;
users[current_user_index].directories[current_directory_index].files[file_index].content = malloc(sizeof(char));
printf("File opened\n");
}
void close() {
if (current_directory_index == -1) {
print_error("No directory selected");
return;
}
char file_name[MAX_FILE_NAME_LENGTH];
printf("Enter file name: ");
scanf("%s", file_name);
int file_index = -1;
for (int i = 0; i < users[current_user_index].directories[current_directory_index].file_count; i++) {
if (strcmp(users[current_user_index].directories[current_directory_index].files[i].name, file_name) == 0) {
file_index = i;
break;
}
}
if (file_index == -1) {
print_error("File not found");
return;
}
if (!users[current_user_index].directories[current_directory_index].files[file_index].is_open) {
print_error("File is not open");
return;
}
users[current_user_index].directories[current_directory_index].files[file_index].is_open = 0;
free(users[current_user_index].directories[current_directory_index].files[file_index].content);
printf("File closed\n");
}
void read() {
if (current_directory_index == -1) {
print_error("No directory selected");
return;
}
char file_name[MAX_FILE_NAME_LENGTH];
printf("Enter file name: ");
scanf("%s", file_name);
int file_index = -1;
for (int i = 0; i < users[current_user_index].directories[current_directory_index].file_count; i++) {
if (strcmp(users[current_user_index].directories[current_directory_index].files[i].name, file_name) == 0) {
file_index = i;
break;
}
}
if (file_index == -1) {
print_error("File not found");
return;
}
if (!users[current_user_index].directories[current_directory_index].files[file_index].is_open) {
print_error("File is not open");
return;
}
if (users[current_user_index].directories[current_directory_index].files[file_index].protection == 1) {
print_error("You do not have permission to read this file");
return;
}
printf("%s\n", users[current_user_index].directories[current_directory_index].files[file_index].content);
}
void write() {
if (current_directory_index == -1) {
print_error("No directory selected");
return;
}
char file_name[MAX_FILE_NAME_LENGTH];
printf("Enter file name: ");
scanf("%s", file_name);
int file_index = -1;
for (int i = 0; i < users[current_user_index].directories[current_directory_index].file_count; i++) {
if (strcmp(users[current_user_index].directories[current_directory_index].files[i].name, file_name) == 0) {
file_index = i;
break;
}
}
if (file_index == -1) {
print_error("File not found");
return;
}
if (!users[current_user_index].directories[current_directory_index].files[file_index].is_open) {
print_error("File is not open");
return;
}
if (users[current_user_index].directories[current_directory_index].files[file_index].protection == 1) {
print_error("You do not have permission to write to this file");
return;
}
char *content = malloc(sizeof(char));
printf("Enter file content: ");
scanf("%s", content);
users[current_user_index].directories[current_directory_index].files[file_index].content = content;
users[current_user_index].directories[current_directory_index].files[file_index].length = strlen(content);
print_success("File written");
}
int main() {
while (1) {
printf("Enter command: ");
char command[MAX_FILE_NAME_LENGTH];
scanf("%s", command);
if (strcmp(command, "login") == 0) {
login();
} else if (strcmp(command, "logout") == 0) {
logout();
} else if (strcmp(command, "select") == 0) {
select_directory();
} else if (strcmp(command, "dir") == 0) {
dir();
} else if (strcmp(command, "create") == 0) {
create();
} else if (strcmp(command, "delete") == 0) {
delete();
} else if (strcmp(command, "open") == 0) {
open();
} else if (strcmp(command, "close") == 0) {
close();
} else if (strcmp(command, "read") == 0) {
read();
} else if (strcmp(command, "write") == 0) {
write();
} else {
print_error("Command not recognized");
}
}
return 0;
}
```
注意:这只是一个简单的示例程序,实际的文件管理系统可能更为复杂,需要考虑更多的安全性和性能问题。
阅读全文