当前位置: 首页 > news >正文

【Linux】设计文件系统(C实现)

要求:

        (1)可以实现下列几条命令 

                dir     列文件目录
                create  创建文件
                delete  删除文件
                read    读文件        
                write   写文件

        (2)列目录时要列出文件名、存取权限(八进制)、文件长度、时间(创建时间,修改时间以及最后一次访问时间);

        (3)源文件可以进行读写保护。

代码:

定义结构体

typedef struct {char name[MAX_NAME_LEN];//最大文件数int permission;//权限int size;//大小  char content[MAX_CONTENT_LEN];//内容char create_time[20];  //创建时间char modify_time[20];  //修改时间char access_time[20];  //最后一次访问时间
} File;

获取当前时间(在显示创建时间,修改时间和访问时间时使用,用于记录当前时间)

//获取当前时间
void get_current_time(char *buffer) {time_t now = time(NULL);struct tm *t = localtime(&now);strftime(buffer, 20, "%Y-%m-%d %H:%M:%S", t);
}

创建文件(创建时注意要赋权)

void create_file() {if (file_count >= MAX_FILES) {printf("文件数量已达上限,无法创建新文件。\n");return;}char name[MAX_NAME_LEN];int permission;printf("请输入文件名: ");scanf("%s", name);printf("请输入文件权限(八进制): ");scanf("%o", &permission);for (int i = 0; i < file_count; i++) {if (strcmp(file_system[i].name, name) == 0) {printf("文件已存在。\n");return;}}File new_file;strcpy(new_file.name, name);new_file.permission = permission;new_file.size = 0;new_file.content[0] = '\0';get_current_time(new_file.create_time);strcpy(new_file.modify_time, new_file.create_time);strcpy(new_file.access_time, new_file.create_time); file_system[file_count++] = new_file;printf("文件创建成功。\n");
}

删除文件

void create_file() {if (file_count >= MAX_FILES) {printf("文件数量已达上限,无法创建新文件。\n");return;}char name[MAX_NAME_LEN];int permission;printf("请输入文件名: ");scanf("%s", name);printf("请输入文件权限(八进制): ");scanf("%o", &permission);for (int i = 0; i < file_count; i++) {if (strcmp(file_system[i].name, name) == 0) {printf("文件已存在。\n");return;}}File new_file;strcpy(new_file.name, name);new_file.permission = permission;new_file.size = 0;new_file.content[0] = '\0';get_current_time(new_file.create_time);strcpy(new_file.modify_time, new_file.create_time);strcpy(new_file.access_time, new_file.create_time); file_system[file_count++] = new_file;printf("文件创建成功。\n");
}

读文件

void read_file() {char name[MAX_NAME_LEN];printf("请输入要读取的文件名: ");scanf("%s", name);for (int i = 0; i < file_count; i++) {if (strcmp(file_system[i].name, name) == 0) {printf("文件内容:\n%s\n", file_system[i].content);get_current_time(file_system[i].access_time);  return;}}printf("文件未找到。\n");
}

写文件

void write_file() {char name[MAX_NAME_LEN];char content[MAX_CONTENT_LEN];printf("请输入要写入的文件名: ");scanf("%s", name);for (int i = 0; i < file_count; i++) {if (strcmp(file_system[i].name, name) == 0) {printf("请输入文件内容: ");getchar(); fgets(content, MAX_CONTENT_LEN, stdin);content[strcspn(content, "\n")] = '\0'; strcpy(file_system[i].content, content);file_system[i].size = strlen(content);get_current_time(file_system[i].modify_time);printf("文件写入成功。\n");return;}}printf("文件未找到。\n");
}

列出文件

void list_files() {if (file_count == 0) {printf("目录为空。\n");return;}printf("%-20s %-10s %-10s %-20s %-20s %-20s\n","文件名", "权限", "大小", "创建时间", "修改时间", "访问时间");for (int i = 0; i < file_count; i++) {printf("%-20s %-10o %-10d %-20s %-20s %-20s\n",file_system[i].name,file_system[i].permission,file_system[i].size,file_system[i].create_time,file_system[i].modify_time,file_system[i].access_time);}
}

目录

void menu() {printf("\n==== 文件系统 ====\n");printf("1. 列文件目录\n");printf("2. 创建文件\n");printf("3. 删除文件\n");printf("4. 读文件\n");printf("5. 写文件\n");printf("6. 退出\n");printf("===================\n");
}

main

int main() {int choice;while (1) {menu();printf("请输入选项: ");scanf("%d", &choice);switch (choice) {case 1:list_files();break;case 2:create_file();break;case 3:delete_file();break;case 4:read_file();break;case 5:write_file();break;case 6:printf("退出系统。\n");return 0;default:printf("无效选项,请重新输入。\n");}}
}

各个部分的代码都已分别给出,可自行在此程序上加入自己的逻辑。

完整代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>#define MAX_FILES 100     //最大文件数
#define MAX_NAME_LEN 50   //文件名字最大长度
#define MAX_CONTENT_LEN 1024 //文件最大内容typedef struct {char name[MAX_NAME_LEN];//最大文件数int permission;//权限int size;//大小  char content[MAX_CONTENT_LEN];//内容char create_time[20];  //创建时间char modify_time[20];  //修改时间char access_time[20];  //最后一次访问时间
} File;File file_system[MAX_FILES];
int file_count = 0;//获取当前时间
void get_current_time(char *buffer) {time_t now = time(NULL);struct tm *t = localtime(&now);strftime(buffer, 20, "%Y-%m-%d %H:%M:%S", t);
}//创建文件
void create_file() {if (file_count >= MAX_FILES) {printf("文件数量已达上限,无法创建新文件。\n");return;}char name[MAX_NAME_LEN];int permission;printf("请输入文件名: ");scanf("%s", name);printf("请输入文件权限(八进制): ");scanf("%o", &permission);for (int i = 0; i < file_count; i++) {if (strcmp(file_system[i].name, name) == 0) {printf("文件已存在。\n");return;}}File new_file;strcpy(new_file.name, name);new_file.permission = permission;new_file.size = 0;new_file.content[0] = '\0';get_current_time(new_file.create_time);strcpy(new_file.modify_time, new_file.create_time);strcpy(new_file.access_time, new_file.create_time); file_system[file_count++] = new_file;printf("文件创建成功。\n");
}//删除文件
void delete_file() {char name[MAX_NAME_LEN];printf("请输入要删除的文件名: ");scanf("%s", name);for (int i = 0; i < file_count; i++) {if (strcmp(file_system[i].name, name) == 0) {for (int j = i; j < file_count - 1; j++) {file_system[j] = file_system[j + 1];}file_count--;printf("文件删除成功。\n");return;}}printf("文件未找到。\n");
}//读文件
void read_file() {char name[MAX_NAME_LEN];printf("请输入要读取的文件名: ");scanf("%s", name);for (int i = 0; i < file_count; i++) {if (strcmp(file_system[i].name, name) == 0) {printf("文件内容:\n%s\n", file_system[i].content);get_current_time(file_system[i].access_time);  return;}}printf("文件未找到。\n");
}//写文件
void write_file() {char name[MAX_NAME_LEN];char content[MAX_CONTENT_LEN];printf("请输入要写入的文件名: ");scanf("%s", name);for (int i = 0; i < file_count; i++) {if (strcmp(file_system[i].name, name) == 0) {printf("请输入文件内容: ");getchar(); fgets(content, MAX_CONTENT_LEN, stdin);content[strcspn(content, "\n")] = '\0'; strcpy(file_system[i].content, content);file_system[i].size = strlen(content);get_current_time(file_system[i].modify_time);printf("文件写入成功。\n");return;}}printf("文件未找到。\n");
}//列出文件
void list_files() {if (file_count == 0) {printf("目录为空。\n");return;}printf("%-20s %-10s %-10s %-20s %-20s %-20s\n","文件名", "权限", "大小", "创建时间", "修改时间", "访问时间");for (int i = 0; i < file_count; i++) {printf("%-20s %-10o %-10d %-20s %-20s %-20s\n",file_system[i].name,file_system[i].permission,file_system[i].size,file_system[i].create_time,file_system[i].modify_time,file_system[i].access_time);}
}//目录
void menu() {printf("\n==== 文件系统 ====\n");printf("1. 列文件目录\n");printf("2. 创建文件\n");printf("3. 删除文件\n");printf("4. 读文件\n");printf("5. 写文件\n");printf("6. 退出\n");printf("===================\n");
}int main() {int choice;while (1) {menu();printf("请输入选项: ");scanf("%d", &choice);switch (choice) {case 1:list_files();break;case 2:create_file();break;case 3:delete_file();break;case 4:read_file();break;case 5:write_file();break;case 6:printf("退出系统。\n");return 0;default:printf("无效选项,请重新输入。\n");}}
}

运行结果(在虚拟机上运行)

1).创建文件

2).列出文件

3).写文件

4).读文件

5).删除文件

6).删除文件

小结:

        首先注意此代码在linux中编译时可能会报错如下:

因为我的代码中使用了 C99 标准引入的特性——for 循环中声明变量。然而,编译器默认未启用 C99 模式,因此报错。 

解决方法:在编译时指定用c99模式,因为 C99 是现代 C 标准,支持更多特性,所以我没有考虑将代码切换兼容 C89。

列表显示时对齐问题,占位符有讲究(为了尽量得体的显示)(给出的代码的占位符都是设计的尽量显示正常的)

开始时时间都无法在同一行显示,显然有问题

那么代码就能正常运行啦,但是因为博主技术水平问题,只能写出这样的代码供大家参考。

http://www.lryc.cn/news/496881.html

相关文章:

  • 详解Rust多线程编程
  • el-upload上传多个文件,一次请求,Django接收
  • Python实现网站资源批量下载【可转成exe程序运行】
  • 《JavaScript高级程序设计》读书笔记 20
  • ASP.NET Core项目中使用SqlSugar连接多个数据库的方式
  • Java面试八股文(精选、纯手打)
  • 工程设计行业内外网文件交换解决方案:FileLink助力高效、安全的跨网协作
  • Qt 2D绘图之三:绘制文字、路径、图像、复合模式
  • 配置宝塔php curl 支持http/2 发送苹果apns消息推送
  • Redis服务配置文件 redis.conf 更新修改配置参数说明
  • Android 俩个主题的不同之处 “Theme.AppCompat vs android:Theme.Material.Light.NoActionBar”
  • Redis+Caffeine 多级缓存数据一致性解决方案
  • vscode ctrl+/注释不了css
  • 《山海经》:北山
  • oracle中删除指定前缀的表
  • 解决 Flutter Dio合并请求多个接口,如果一个接口500,那么导致其他请求不在执行
  • The selected directory is not a valid home for Go SDK
  • 基于云模型的车辆行驶速度估计算法matlab仿真
  • MySQL有哪些日志?
  • Axios:现代JavaScript HTTP客户端
  • python学opencv|读取视频(一)灰度视频制作和保存
  • 【Rust WebAssembly 入门实操遇到的问题】
  • 掌握CMake中的变量:设置、使用及实际应用示例详解
  • React基础知识三 router路由全指南
  • [VUE]框架网页开发02-如何打包Vue.js框架网页并在服务器中通过Tomcat启动
  • k8s Quality of Service
  • 顶刊算法 | 鱼鹰算法OOA-BiTCN-BiGRU-Attention多输入单输出回归预测(Maltab)
  • 什么语言适合做 Serverless 开发?
  • 使用OpenCV和卡尔曼滤波器进行实时活体检测
  • 【25春招前端八股文】——JS数据类型检测方式