用C语言写一个简单的文件系统,代码要求:具备基本的文件处理功能,包括实现文件的建立、打开、删除、关闭、复制、读、写、查询等功能,附带菜单栏。
时间: 2023-10-20 18:14:03 浏览: 50
这是一个比较复杂的项目,需要涉及到文件操作、数据结构等多方面的知识。以下是一个简单的示例代码,只实现了部分功能,仅供参考。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_FILENAME_LEN 20
#define MAX_FILE_NUM 10
#define BLOCK_SIZE 1024
#define BLOCK_NUM 100
#define MAX_DATA_LEN (BLOCK_SIZE - sizeof(int))
struct file {
char name[MAX_FILENAME_LEN];
int size;
int block_num;
int block_index[BLOCK_NUM];
};
struct block {
int next;
char data[MAX_DATA_LEN];
};
struct file_system {
struct file files[MAX_FILE_NUM];
int file_num;
struct block blocks[BLOCK_NUM];
int free_block;
};
struct file_system fs;
void init_file_system() {
memset(&fs, 0, sizeof(fs));
fs.free_block = 0;
for (int i = 0; i < BLOCK_NUM; i++) {
fs.blocks[i].next = i + 1;
}
fs.blocks[BLOCK_NUM - 1].next = -1;
}
int find_file(char *name) {
for (int i = 0; i < fs.file_num; i++) {
if (strcmp(fs.files[i].name, name) == 0) {
return i;
}
}
return -1;
}
int find_free_block() {
if (fs.free_block == -1) {
return -1;
}
int block_index = fs.free_block;
fs.free_block = fs.blocks[block_index].next;
return block_index;
}
void free_block(int block_index) {
fs.blocks[block_index].next = fs.free_block;
fs.free_block = block_index;
}
int create_file(char *name) {
if (find_file(name) != -1) {
printf("File %s already exists\n", name);
return -1;
}
if (fs.file_num == MAX_FILE_NUM) {
printf("File system is full\n");
return -1;
}
struct file *file = &fs.files[fs.file_num++];
strcpy(file->name, name);
file->size = 0;
file->block_num = 0;
return fs.file_num - 1;
}
int delete_file(char *name) {
int file_index = find_file(name);
if (file_index == -1) {
printf("File %s not found\n", name);
return -1;
}
struct file *file = &fs.files[file_index];
for (int i = 0; i < file->block_num; i++) {
free_block(file->block_index[i]);
}
memmove(&fs.files[file_index], &fs.files[file_index + 1], (fs.file_num - file_index - 1) * sizeof(struct file));
fs.file_num--;
return 0;
}
int open_file(char *name) {
int file_index = find_file(name);
if (file_index == -1) {
printf("File %s not found\n", name);
return -1;
}
return file_index;
}
int close_file(int file_index) {
return 0;
}
int copy_file(char *src_name, char *dst_name) {
int src_index = find_file(src_name);
if (src_index == -1) {
printf("File %s not found\n", src_name);
return -1;
}
if (find_file(dst_name) != -1) {
printf("File %s already exists\n", dst_name);
return -1;
}
if (fs.file_num == MAX_FILE_NUM) {
printf("File system is full\n");
return -1;
}
struct file *src_file = &fs.files[src_index];
struct file *dst_file = &fs.files[fs.file_num++];
strcpy(dst_file->name, dst_name);
dst_file->size = src_file->size;
dst_file->block_num = src_file->block_num;
for (int i = 0; i < src_file->block_num; i++) {
int block_index = find_free_block();
if (block_index == -1) {
printf("File system is full\n");
return -1;
}
memcpy(&fs.blocks[block_index], &fs.blocks[src_file->block_index[i]], sizeof(struct block));
dst_file->block_index[i] = block_index;
}
return fs.file_num - 1;
}
int read_file(int file_index, char *buf, int len, int offset) {
struct file *file = &fs.files[file_index];
if (offset < 0 || offset >= file->size) {
printf("Invalid offset %d\n", offset);
return -1;
}
int read_len = len;
if (offset + len > file->size) {
read_len = file->size - offset;
}
int read_pos = 0;
int block_offset = offset % BLOCK_SIZE;
int block_index = file->block_index[offset / BLOCK_SIZE];
while (read_pos < read_len) {
int block_len = BLOCK_SIZE - block_offset;
if (block_offset + block_len > read_len - read_pos) {
block_len = read_len - read_pos - block_offset;
}
memcpy(buf + read_pos, fs.blocks[block_index].data + block_offset, block_len);
read_pos += block_len;
block_offset = 0;
block_index = fs.blocks[block_index].next;
}
return read_len;
}
int write_file(int file_index, char *buf, int len, int offset) {
struct file *file = &fs.files[file_index];
if (offset < 0 || offset > file->size) {
printf("Invalid offset %d\n", offset);
return -1;
}
int write_len = len;
if (offset + len > MAX_DATA_LEN * file->block_num) {
printf("File is too large\n");
return -1;
}
if (offset + len > file->size) {
file->size = offset + len;
}
int write_pos = 0;
int block_offset = offset % BLOCK_SIZE;
int block_index = file->block_index[offset / BLOCK_SIZE];
while (write_pos < write_len) {
int block_len = BLOCK_SIZE - block_offset;
if (block_offset + block_len > write_len - write_pos) {
block_len = write_len - write_pos - block_offset;
}
memcpy(fs.blocks[block_index].data + block_offset, buf + write_pos, block_len);
write_pos += block_len;
block_offset = 0;
if (write_pos < write_len) {
int next_block_index = find_free_block();
if (next_block_index == -1) {
printf("File system is full\n");
return -1;
}
fs.blocks[block_index].next = next_block_index;
block_index = next_block_index;
file->block_num++;
}
}
return write_len;
}
void list_files() {
printf("Name\tSize\n");
for (int i = 0; i < fs.file_num; i++) {
printf("%s\t%d\n", fs.files[i].name, fs.files[i].size);
}
}
void print_menu() {
printf("1. Create file\n");
printf("2. Delete file\n");
printf("3. Open file\n");
printf("4. Close file\n");
printf("5. Copy file\n");
printf("6. Read file\n");
printf("7. Write file\n");
printf("8. List files\n");
printf("9. Exit\n");
}
int main() {
init_file_system();
char buf[BLOCK_SIZE];
int file_index = -1;
while (1) {
print_menu();
int choice;
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter file name: ");
scanf("%s", buf);
create_file(buf);
break;
case 2:
printf("Enter file name: ");
scanf("%s", buf);
delete_file(buf);
break;
case 3:
printf("Enter file name: ");
scanf("%s", buf);
file_index = open_file(buf);
if (file_index == -1) {
break;
}
printf("File %s opened\n", buf);
break;
case 4:
close_file(file_index);
file_index = -1;
printf("File closed\n");
break;
case 5:
printf("Enter source file name: ");
scanf("%s", buf);
printf("Enter destination file name: ");
scanf("%s", buf + MAX_FILENAME_LEN);
copy_file(buf, buf + MAX_FILENAME_LEN);
break;
case 6:
printf("Enter offset and length: ");
int offset, len;
scanf("%d%d", &offset, &len);
if (file_index == -1) {
printf("No file opened\n");
break;
}
read_file(file_index, buf, len, offset);
printf("Data read: %.*s\n", len, buf);
break;
case 7:
printf("Enter offset and data: ");
scanf("%d%s", &offset, buf);
if (file_index == -1) {
printf("No file opened\n");
break;
}
write_file(file_index, buf, strlen(buf), offset);
printf("Data written\n");
break;
case 8:
list_files();
break;
case 9:
exit(0);
break;
default:
printf("Invalid choice\n");
break;
}
}
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)