#include #include #include #define MAX_LENGTH 1000 int main() { char filename[50]; char text[MAX_LENGTH]; char command[20]; FILE *file; int length = 0; printf("请输入文件名:"); scanf("%s", filename); file = fopen(filename, "r"); if (file == NULL) { file = fopen(filename, "w"); fclose(file); } while (1) { printf("\n请输入命令(输入'quit'退出):"); scanf("%s", command); if (strcmp(command, "quit") == 0) { break; } if (strcmp(command, "open") == 0) { printf("请输入要打开的文件名:"); scanf("%s", filename); file = fopen(filename, "r"); if (file == NULL) { printf("文件不存在!\n"); continue; } fseek(file, 0, SEEK_END); length = ftell(file); fseek(file, 0, SEEK_SET); fread(text, sizeof(char), length, file); text[length] = '\0'; fclose(file); printf("文件内容:\n%s\n", text); } if (strcmp(command, "save") == 0) { printf("请输入要保存的文件名:"); scanf("%s", filename); file = fopen(filename, "w"); if (file == NULL) { printf("无法保存文件!\n"); continue; } fprintf(file, "%s", text); fclose(file); printf("文件保存成功!\n"); } if (strcmp(command, "edit") == 0) { printf("请输入要编辑的内容:"); scanf(" %[^\n]s", text); length = strlen(text); if (length > MAX_LENGTH - 1) { printf("输入内容过长!\n"); continue; } text[length] = '\0'; printf("编辑后的内容:\n%s\n", text); } } return 0; } 这个示例程序实现了一个简单的文本编辑器,支持打开文件、保存文件和编辑文本。你可以根据自己的需求进一步完善和扩展这个程序,例如添加复制、粘贴、撤销、重做等功能。 请注意,这只是一个基本框架,实际的文本编辑器需要更多的代码和逻辑来处理各种情况。如果你有兴趣开发一个完整的文本编辑器,建议参考现有的开源文本编辑器项目,学习它们的实现方式和技巧。