文件管理器200行小代码加流程图
时间: 2023-09-09 12:12:40 浏览: 49
以下是一个简单的 C 语言文件管理器的代码示例和流程图,代码共 200 行左右。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_FILES 100
struct File {
char name[20];
char content[100];
};
struct File file_list[MAX_FILES];
int file_count = 0;
void create_file() {
if (file_count >= MAX_FILES) {
printf("Error: too many files!\n");
return;
}
struct File file;
printf("Enter file name: ");
scanf("%s", file.name);
printf("Enter file content: ");
scanf("%s", file.content);
file_list[file_count++] = file;
printf("Successfully created file!\n");
}
void update_file() {
if (file_count == 0) {
printf("Error: no files!\n");
return;
}
char name[20];
printf("Enter file name to update: ");
scanf("%s", name);
int index = -1;
for (int i = 0; i < file_count; i++) {
if (strcmp(file_list[i].name, name) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("Error: file not found!\n");
return;
}
printf("Enter new content: ");
scanf("%s", file_list[index].content);
printf("Successfully updated file!\n");
}
void delete_file() {
if (file_count == 0) {
printf("Error: no files!\n");
return;
}
char name[20];
printf("Enter file name to delete: ");
scanf("%s", name);
int index = -1;
for (int i = 0; i < file_count; i++) {
if (strcmp(file_list[i].name, name) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("Error: file not found!\n");
return;
}
for (int i = index; i < file_count - 1; i++) {
file_list[i] = file_list[i + 1];
}
file_count--;
printf("Successfully deleted file!\n");
}
void print_file(struct File file) {
printf("Name: %s\n", file.name);
printf("Content: %s\n", file.content);
}
void print_all_files() {
if (file_count == 0) {
printf("No files!\n");
return;
}
for (int i = 0; i < file_count; i++) {
printf("File %d:\n", i + 1);
print_file(file_list[i]);
printf("\n");
}
}
int main() {
int choice;
while (1) {
printf("1. Create file\n");
printf("2. Update file\n");
printf("3. Delete file\n");
printf("4. Print all files\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
create_file();
break;
case 2:
update_file();
break;
case 3:
delete_file();
break;
case 4:
print_all_files();
break;
case 5:
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
```
以下是流程图:
![文件管理器流程图](https://i.imgur.com/mBvDj4v.png)