网站备案的意思,dw个人网站制作模板,口碑营销案例ppt,百度提交网站收录入口要求#xff1a; (1)可以实现下列几条命令 dir 列文件目录 create 创建文件 delete 删除文件 read 读文件 write 写文件 (2)列目录时要列出文件名、存取权限#xff08;八进制#xff09;、文件长度、时间#xff08;创建时间#xff0c;修改时间以及…要求 (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。 列表显示时对齐问题占位符有讲究为了尽量得体的显示给出的代码的占位符都是设计的尽量显示正常的
开始时时间都无法在同一行显示显然有问题 那么代码就能正常运行啦但是因为博主技术水平问题只能写出这样的代码供大家参考。