用c语言具体实现要求: a.新建记事本文件:输入文件名,输入文本,输入结束符结束输入。结束回到主界面; b.查看已保存的记事本文件:输入文件名,查看文本。结束回到主界面。 c.删除记事本文件:输入文件名,删除记事本文件。结束返回主界面。 d.编辑记事本内容:输入文件名,使用方向键(或自定义键)移动光标位置,插入、删除、修改记录事本内容。 e.退出程序。
时间: 2024-03-25 11:37:05 浏览: 77
好的,下面是一个简单的记事本程序的具体实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define MAX_FILE_NAME_LENGTH 255
#define MAX_FILE_CONTENT_LENGTH 1024
// 新建记事本文件
void createFile() {
char fileName[MAX_FILE_NAME_LENGTH + 1];
char fileContent[MAX_FILE_CONTENT_LENGTH + 1];
FILE *fp;
printf("请输入文件名:");
scanf("%s", fileName);
fp = fopen(fileName, "w");
if (fp == NULL) {
printf("无法创建文件\n");
return;
}
printf("请输入文本,按Ctrl+z结束输入:\n");
while (fgets(fileContent, MAX_FILE_CONTENT_LENGTH, stdin) != NULL) {
fprintf(fp, "%s", fileContent);
}
fclose(fp);
printf("文件创建成功\n");
}
// 查看已保存的记事本文件
void viewFile() {
char fileName[MAX_FILE_NAME_LENGTH + 1];
char fileContent[MAX_FILE_CONTENT_LENGTH];
FILE *fp;
printf("请输入文件名:");
scanf("%s", fileName);
fp = fopen(fileName, "r");
if (fp == NULL) {
printf("无法打开文件\n");
return;
}
printf("文件内容如下:\n");
while (fgets(fileContent, MAX_FILE_CONTENT_LENGTH, fp) != NULL) {
printf("%s", fileContent);
}
fclose(fp);
}
// 删除记事本文件
void deleteFile() {
char fileName[MAX_FILE_NAME_LENGTH + 1];
int ret;
printf("请输入文件名:");
scanf("%s", fileName);
ret = remove(fileName);
if (ret == 0) {
printf("文件删除成功\n");
} else {
printf("无法删除文件\n");
}
}
// 编辑记事本内容
void editFile() {
char fileName[MAX_FILE_NAME_LENGTH + 1];
char fileContent[MAX_FILE_CONTENT_LENGTH];
int i, j, ch, curPos, endPos;
FILE *fp;
printf("请输入文件名:");
scanf("%s", fileName);
fp = fopen(fileName, "r+");
if (fp == NULL) {
printf("无法打开文件\n");
return;
}
printf("文件内容如下:\n");
while (fgets(fileContent, MAX_FILE_CONTENT_LENGTH, fp) != NULL) {
printf("%s", fileContent);
}
curPos = endPos = ftell(fp);
printf("\n");
while ((ch = getch()) != 27) {
switch (ch) {
case 72: // 上箭头
if (curPos > 0) {
curPos--;
fseek(fp, curPos, SEEK_SET);
}
break;
case 75: // 左箭头
if (curPos > 0) {
curPos--;
fseek(fp, curPos, SEEK_SET);
}
break;
case 77: // 右箭头
if (curPos < endPos) {
curPos++;
fseek(fp, curPos, SEEK_SET);
}
break;
case 80: // 下箭头
if (curPos < endPos) {
curPos++;
fseek(fp, curPos, SEEK_SET);
}
break;
case 8: // Backspace
if (curPos > 0) {
fseek(fp, curPos - 1, SEEK_SET);
endPos--;
for (i = curPos - 1; i < endPos; i++) {
fileContent[i] = fileContent[i + 1];
}
fileContent[endPos] = '\0';
fputs(fileContent, fp);
fseek(fp, curPos - 1, SEEK_SET);
}
break;
default: // 其他字符
if (curPos < MAX_FILE_CONTENT_LENGTH) {
endPos++;
for (i = endPos - 1; i > curPos; i--) {
fileContent[i] = fileContent[i - 1];
}
fileContent[curPos] = ch;
fileContent[endPos] = '\0';
fputs(fileContent + curPos, fp);
curPos++;
fseek(fp, curPos, SEEK_SET);
}
break;
}
}
fclose(fp);
}
int main() {
char ch;
while (1) {
printf("请选择功能:\n");
printf("1. 新建记事本文件\n");
printf("2. 查看已保存的记事本文件\n");
printf("3. 删除记事本文件\n");
printf("4. 编辑记事本内容\n");
printf("5. 退出程序\n");
ch = getch();
switch (ch) {
case '1':
createFile();
break;
case '2':
viewFile();
break;
case '3':
deleteFile();
break;
case '4':
editFile();
break;
case '5':
exit(0);
break;
default:
printf("无效选项,请重新选择\n");
break;
}
}
return 0;
}
```
这个程序实现了您所要求的所有功能。用户可以通过输入数字来选择不同的功能,按照提示输入文件名和文本来创建和编辑记事本文件,使用方向键来移动光标,使用Backspace键来删除字符,按下Esc键退出编辑模式,按下Ctrl+z键结束文本输入,按下数字5退出程序。
阅读全文