FAT 文件系统模拟设计与实现:C语言程序模拟FAT16

实验描述

利用标准C 语言,模拟实现 FAT 16文件系统,支持 FAT 文件系统格式的模拟磁盘卷及其中的目录与文件的存取操作,包括磁盘卷格式化、创建目录、改变当前目录、重命名目录、显示目录、删除目录、创建空文件、重命名文件、写文件、显示文件内容、删除文件。

用到的数据结构

  1. 路径存储,存储卷的名字和当前的目录,存储目录的起点和空闲块。当卷没有初始化即卷名字为空时可以实现报错 
    typedef struct {char filename[MAX_FILENAME_LENGTH];int start_block;int size;char data[MAX_FILE_SIZE];
    } DirectoryEntry;
  2. 文件的数据结构,存储文件的起点和占用的块的大小,data存储文件内部的数据
    typedef struct {char volume_name[MAX_FILENAME_LENGTH];int total_blocks;int free_blocks;DirectoryEntry* directory;char current_path[MAX_FILENAME_LENGTH];
    } FileSystem;

磁盘使用模拟

分别用函数实现磁盘卷格式化、创建目录、改变当前目录、重命名目录、显示目录、删除目录、创建空文件、重命名文件、写文件、显示文件内容、删除文件。列表编号每个操作,输入对应的序号选择操作并且进行交互,遇到一些错误则会有提示,模拟结束可以选择退出的操作。

运行示例

卷的初始化:输入卷名字和空间大小,即磁盘的格式化

创建目录user1

进入目录user1,此处若没有写对目录名字则会报错

创建新文件story

查看目录

写文件story,需要在此处输入有效的文件名

查看文件story,需要在此处输入有效的文件名

完整程序 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_FILENAME_LENGTH 16
#define MAX_FILE_SIZE 1024typedef struct {char filename[MAX_FILENAME_LENGTH];int start_block;int size;char data[MAX_FILE_SIZE];
} DirectoryEntry;typedef struct {char volume_name[MAX_FILENAME_LENGTH];int total_blocks;int free_blocks;DirectoryEntry* directory;char current_path[MAX_FILENAME_LENGTH];
} FileSystem;FileSystem fs;void formatDisk() {printf("Enter volume name: ");scanf("%s", fs.volume_name);printf("Enter total number of blocks: ");scanf("%d", &fs.total_blocks);fs.free_blocks = fs.total_blocks;fs.directory = (DirectoryEntry*)malloc(sizeof(DirectoryEntry) * fs.total_blocks);for (int i = 0; i < fs.total_blocks; i++) {strcpy(fs.directory[i].filename, "");fs.directory[i].start_block = -1;fs.directory[i].size = 0;}strcpy(fs.current_path, "/");printf("Disk formatted successfully.\n");
}void createDirectory() {char dirname[MAX_FILENAME_LENGTH];printf("Enter directory name: ");scanf("%s", dirname);for (int i = 0; i < fs.total_blocks; i++) {if (strcmp(fs.directory[i].filename, "") == 0) {strcpy(fs.directory[i].filename, dirname);fs.directory[i].start_block = -1;fs.directory[i].size = 0;printf("Directory created successfully.\n");return;}}printf("No free space for directory creation.\n");
}void changeDirectory() {char dirname[MAX_FILENAME_LENGTH];printf("Enter directory name: ");scanf("%s", dirname);for (int i = 0; i < fs.total_blocks; i++) {if (strcmp(fs.directory[i].filename, dirname) == 0) {strcat(fs.current_path, dirname);strcat(fs.current_path, "/");printf("Directory changed to '%s'.\n", fs.current_path);return;}}printf("Directory '%s' not found.\n", dirname);
}void renameDirectory() {char oldname[MAX_FILENAME_LENGTH], newname[MAX_FILENAME_LENGTH];printf("Enter old directory name: ");scanf("%s", oldname);printf("Enter new directory name: ");scanf("%s", newname);for (int i = 0; i < fs.total_blocks; i++) {if (strcmp(fs.directory[i].filename, oldname) == 0) {strcpy(fs.directory[i].filename, newname);printf("Directory renamed successfully.\n");return;}}printf("Directory '%s' not found.\n", oldname);
}void displayDirectory() {printf("Current Disk: %s\n", fs.volume_name);printf("Current Path: %s\n", fs.current_path);printf("Directory listing:\n");for (int i = 0; i < fs.total_blocks; i++) {if (strcmp(fs.directory[i].filename, "") != 0) {printf("- %s\n", fs.directory[i].filename);}}
}void deleteDirectory() {char dirname[MAX_FILENAME_LENGTH];printf("Enter directory name: ");scanf("%s", dirname);for (int i = 0; i < fs.total_blocks; i++) {if (strcmp(fs.directory[i].filename, dirname) == 0) {strcpy(fs.directory[i].filename, "");printf("Directory deleted successfully.\n");return;}}printf("Directory '%s' not found.\n", dirname);
}void createFile() {char filename[MAX_FILENAME_LENGTH];printf("Enter file name: ");scanf("%s", filename);for (int i = 0; i < fs.total_blocks; i++) {if (strcmp(fs.directory[i].filename, "") == 0) {strcpy(fs.directory[i].filename, filename);fs.directory[i].start_block = i;fs.directory[i].size = 0;fs.directory[i].data[0] = '\0'; fs.free_blocks--;printf("File created successfully.\n");return;}}printf("No free space for file creation.\n");
}void renameFile() {char oldname[MAX_FILENAME_LENGTH], newname[MAX_FILENAME_LENGTH];printf("Enter old file name: ");scanf("%s", oldname);printf("Enter new file name: ");scanf("%s", newname);for (int i = 0; i < fs.total_blocks; i++) {if (strcmp(fs.directory[i].filename, oldname) == 0) {strcpy(fs.directory[i].filename, newname);printf("File renamed successfully.\n");return;}}printf("File '%s' not found.\n", oldname);
}void writeFile() {char filename[MAX_FILENAME_LENGTH];printf("Enter file name: ");scanf("%s", filename);for (int i = 0; i < fs.total_blocks; i++) {if (strcmp(fs.directory[i].filename, filename) == 0) {int block_index = fs.directory[i].start_block;if (block_index == -1) {printf("File '%s' not found.\n", filename);return;}printf("Enter file content:\n");char content[MAX_FILE_SIZE];scanf("%s", content);strcpy(fs.current_path, filename);fs.directory[i].size = strlen(content);strcpy(fs.directory[i].data, content); printf("File '%s' written successfully.\n", filename);return;}}printf("File '%s' not found.\n", filename);
}void displayFileContent() {char filename[MAX_FILENAME_LENGTH];printf("Enter file name: ");scanf("%s", filename);for (int i = 0; i < fs.total_blocks; i++) {if (strcmp(fs.directory[i].filename, filename) == 0) {printf("Content of file '%s':\n%s\n", filename, fs.directory[i].data);return;}}printf("File '%s' not found.\n", filename);
}void deleteFile() {char filename[MAX_FILENAME_LENGTH];printf("Enter file name: ");scanf("%s", filename);for (int i = 0; i < fs.total_blocks; i++) {if (strcmp(fs.directory[i].filename, filename) == 0) {strcpy(fs.directory[i].filename, "");fs.directory[i].start_block = -1;fs.directory[i].size = 0;fs.free_blocks++;printf("File deleted successfully.\n");return;}}printf("File '%s' not found.\n", filename);
}int main() {int choice;printf("----------------------------------- FAT16 File System ----------------------------------\n");printf("1. Format Disk         2. Create Directory    3. Change Directory    4. Rename Directory\n");printf("5. Display Directory   6. Delete Directory    7. Create File         8. Rename File\n");printf("9. Write File         10. Display File       11. Delete File         0. Exit\n");do {printf("\nCurrent Disk: %s   Current Path: %s\n", fs.volume_name, fs.current_path);  printf("Enter your choice: ");scanf("%d", &choice);switch (choice) {case 1: formatDisk(); break;case 2: createDirectory(); break;case 3: changeDirectory(); break;case 4: renameDirectory(); break;case 5: displayDirectory(); break;case 6: deleteDirectory(); break;case 7: createFile(); break;case 8: renameFile(); break;case 9: writeFile(); break;case 10: displayFileContent(); break;case 11: deleteFile(); break;case 0: break;default: printf("Invalid choice.\n");}} while (choice != 0);free(fs.directory);return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/291598.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

gem5 garnet 拓扑结构之port: NI CPU ROUTER L1 L2

简介 有Crossbar&#xff0c;CrossbarGarnet&#xff0c;Mesh_*&#xff0c;MeshDirCorners_XY&#xff0c;Pt2Pt等拓扑结构&#xff0c;我们主要关注mesh-xy。参考是https://www.gem5.org/documentation/general_docs/ruby/interconnection-network/ MESI TWO LEVEL与 mesh …

ASP.NET Core基础之定时任务(二)-Quartz.NET入门

阅读本文你的收获 了解任务调度框架QuartZ.NET的核心构成学会在ASP.NET Core 中使用QuartZ.NET 在项目的开发过程中&#xff0c;难免会遇见需要后台处理的任务&#xff0c;例如定时发送邮件通知、后台处理耗时的数据处理等&#xff0c;上次分享了ASP.NET Core中实现定时任务的…

STM32G4x FLASH 读写(修改HAL库驱动)

主要工作就是把HAL的超时用LL库延时替代&#xff0c;保留了中断擦写模式、轮询等待擦写&#xff0c;待验证哈。 笔者用的芯片为STM32G473CBT6 128KB Flash&#xff0c;开环环境为CUBEMXMDK5.32&#xff0c;因为G4已经没有标准库了&#xff0c;笔者还是习惯使用标准库的开发方式…

Flink 状态管理与容错机制(CheckPoint SavePoint)的关系

一、什么是状态 无状态计算的例子&#xff1a; 例如一个加法算子&#xff0c;第一次输入235那么以后我多次数据23的时候得到的结果都是5。得出的结论就是&#xff0c;相同的输入都会得到相同的结果&#xff0c;与次数无关。 有状态计算的例子&#xff1a; 访问量的统计&#x…

微信小程序开发系列-01创建一个最小的小程序项目

本文讲述了通过微信开发者工具&#xff0c;创建一个新的小程序项目&#xff0c;完全从零开始&#xff0c;不依赖开发者工具的模板。目的是为了更好的理解小程序工程项目的构成。 文章目录 创建一个空项目app.json全局配置pagessitemapLocation app.js 创建一个空项目 打开微信…

【C语言】指针详解(一)

目录 1.内存和地址 1.1内存 1.2如何理解编址 2.指针变量和地址 2.1取地址操作符&#xff08;&&#xff09; 2.2指针变量和解引用操作符&#xff08;*&#xff09; 2.2.1指针变量 2.2.2拆解指针类型 2.2.3解引用操作符 2.3指针变量大小 1.内存和地址 1.1内存 在讲内…

大型医院PACS系统源码,影像存储与传输系统源码,支持多种图像处理及三维重建功能

PACS系统是医院影像科室中应用的一种系统&#xff0c;主要用于获取、传输、存档和处理医学影像。它通过各种接口&#xff0c;如模拟、DICOM和网络&#xff0c;以数字化的方式将各种医学影像&#xff0c;如核磁共振、CT扫描、超声波等保存起来&#xff0c;并在需要时能够快速调取…

模型实战(18)之C++ - tensorRT部署GAN模型实现人脸超分辨重建

模型实战(18)之C++ - tensorRT部署GAN模型实现人脸超分辨重建 一个实现人脸超分辨率重建的demo支持StyleGAN: GPEN or GFPGAN通过C++ - tensorrt 快速部署,推理速度每帧 在RTX3090上5.5ms+,RTX3050上10ms+下边是实现效果(图片来源于网络search,如若侵权,联系删除) 下边…

先进制造身份治理现状洞察:从手动运维迈向自动化身份治理时代

在新一轮科技革命和产业变革的推动下&#xff0c;制造业正面临绿色化、智能化、服务化和定制化发展趋势。为顺应新技术革命及工业发展模式变化趋势&#xff0c;传统工业化理论需要进行修正和创新。其中&#xff0c;对工业化水平的判断标准从以三次产业比重标准为主回归到工业技…

Flink系列之:Savepoints

Flink系列之&#xff1a;Savepoints 一、Savepoints二、分配算子ID三、Savepoint 状态四、算子五、触发Savepoint六、Savepoint 格式七、触发 Savepoint八、使用 YARN 触发 Savepoint九、使用 Savepoint 停止作业十、从 Savepoint 恢复十一、跳过无法映射的状态恢复十二、Resto…

gem5 RubyPort: mem_request_port作用与连接 simple-MI_example.py

简介 回答这个问题&#xff1a;RubyPort的口下&#xff0c;一共定义了六个口&#xff0c;分别是mem_request_port&#xff0c;mem_response_port&#xff0c;pio_request_port&#xff0c;pio_response_port&#xff0c;in_ports, interrupt_out_ports&#xff0c;他们分别有什…

2023 年人工智能研究与技术排名前 10 的国家

人工智能研究是一项全球性的工作。虽然美国和中国因其对人工智能的贡献而备受关注&#xff0c;但事实是&#xff0c;世界各国都在涉足这项技术&#xff0c;尝试新的突破&#xff0c;并吸引投资者的关注。 斯坦福大学的《2023年人工智能报告》估计&#xff0c;到 2022 年&#…