修改这段代码:#include<stdio.h> #include<stdlib.h> #include<string.h> void menu1(); void create_file(FILE*fp,char*fname); void write_file(FILE*fp,char*fname); void read_file(FILE*fp,char*fname); int main() { system("color F4"); FILE *fp; int count=0; char num[100]; int flag1,flag2,flag3; char F_name[20]; while(1) { menu1(); printf("\t请选择你要进行的文件操作:"); scanf("%s",num); getchar(); while(strlen(num)>1||num[0]<49||num[0]>52) { printf("\t输入错误!请重新输入:"); scanf("%s",num); getchar(); } switch(num[0]) { case'1':flag1=1;break; case'2':flag2=1;break; case'3':flag3=1;break; case'4':printf("\n\t");exit(0);break; } if(flag1==1) { flag1=0; create_file(fp,F_name); } else if(flag2==1) { flag2=0; write_file(fp,F_name); } else if(flag3==1) { flag3=0; read_file(fp,F_name); } } return 0; } void menu1() { printf(" \n\n"); printf(" *************************************** \n"); printf("\t \n"); printf(" 文本编辑器 \n"); printf("\t \n "); printf(" *************************************** \n"); printf("\t 1.建立文本 \n"); printf("\t 2.编辑文本 \n"); printf("\t 3.读取文本 \n"); printf("\t 4.退出程序 \n"); printf(" *************************************** \n"); } void write_file(FILE*fp,char*fname) { char ch; printf("\t请输入编辑的文件名:\n\t"); gets(fname); if((fp=fopen(fname,"w"))==NULL) { printf("\t不能打开文件!\n"); exit(0); } printf("\t请输入内容(以#号键结束):\n\t"); ch=getchar(); while(ch!='#') { fputc(ch,fp); ch=getchar(); } printf("\n\n"); getchar(); fclose(fp); } void create_file(FILE*fp,char*fname) { printf("\t请输入新建的文件名:\n\t"); gets(fname); if((fp=fopen(fname,"w+"))==NULL) { printf("\t不能打开文件!\n"); exit(0); } printf("\t%s文件已经创建好!\n\n",fname); fclose(fp); } void read_file(FILE*fp,char*fname) { char ch; printf("\t请输入读取的文件名:\n\t"); gets(fname); if((fp=fopen(fname,"r"))==NULL) { printf("\t不能打开文件!\n"); exit(0); } printf("\t"); while((ch=fgetc(fp))!=EOF) { putchar(ch); } printf("\t\n\n"); fclose(fp); }
时间: 2023-09-08 11:07:54 浏览: 79
在代码中,flag1、flag2、flag3 没有初始化,且没有清零操作,可能会导致程序出现意外的行为。建议在 while 循环开始前将这三个变量初始化为 0。
代码中的变量 F_name 在 main 函数中没有初始化,建议在使用该变量前先进行初始化。
还有一些细节问题,比如在写文件和读文件时,应该加上文件名的长度判断,防止用户输入的文件名过长导致程序出错。
以下是修改后的代码:
相关问题
#include <stdio.h> #include <stdlib.h> #include<string.h> #include<conio.h> typedef struct //磁盘结构体 { char filedata[20][20]; } disk; disk Disk; void menu(); void firstpart();//磁盘初始化 void secondpart(); void thirdpart(); void forthpart(); void fifthpart(); void sixthpart(); void seventhpart(); void goback(void a());
这段代码定义了一个磁盘结构体 `disk`,其中包含一个二维字符数组 `filedata`。同时还定义了一个全局变量 `Disk`,类型为 `disk`,表示磁盘。
此外,还定义了一系列函数,包括 `menu()`、`firstpart()`、`secondpart()`、`thirdpart()`、`forthpart()`、`fifthpart()`、`sixthpart()`、`seventhpart()` 和 `goback()`。这些函数的具体实现需要在代码中找到。
c语言的学生管理系统在此代码基础上写一个删除学生信息#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> typedef struct _student{ int ID; char name[20]; char sex[5]; char Class[20]; char date[20]; char Tel[20]; char home[20]; }student; typedef struc
t _node{ student data; struct _node *next; }node; node *createList() { node *head,*p; head = p = (node*)malloc(sizeof(node)); p->next = NULL; return head; } void addStudent(node *head, student *newStu) { node *p, *q; p = head; while(p->next != NULL) { p = p->next; } q = (node*)malloc(sizeof(node)); q->data = *newStu; q->next = p->next; p->next = q; } void displayList(node *head) { node *p; p = head->next; while(p != NULL) { printf("%d\t%s\t%s\t%s\t%s\t%s\t%s\n", p->data.ID, p->data.name, p->data.sex, p->data.Class, p->data.date, p->data.Tel, p->data.home); p = p->next; } } void deleteStudent(node *head, int ID) { node *p, *q; p = head->next; while(p != NULL && p->data.ID != ID) { q = p; p = p->next; } if(p == NULL) { printf("No student with ID %d exists.\n", ID); } else { q->next = p->next; free(p); printf("Student with ID %d has been deleted.\n", ID); } } int main() { node *head; student newStu; int choice, ID; char name[20], sex[5], Class[20], date[20], Tel[20], home[20]; head = createList(); do { printf("Menu:\n"); printf("1. Add student\n"); printf("2. Display all students\n"); printf("3. Delete student\n"); printf("4. Exit\n"); printf("Enter your choice: "); scanf("%d", &choice); switch(choice) { case 1: printf("Enter student ID: "); scanf("%d", &newStu.ID); printf("Enter student name: "); scanf("%s", &newStu.name); printf("Enter student sex: "); scanf("%s", &newStu.sex); printf("Enter student class: "); scanf("%s", &newStu.Class); printf("Enter student date of birth: "); scanf("%s", &newStu.date); printf("Enter student telephone number: "); scanf("%s", &newStu.Tel); printf("Enter student home address: "); scanf("%s", &newStu.home); addStudent(head, &newStu); break; case 2: displayList(head); break; case 3: printf("Enter student ID to delete: "); scanf("%d", &ID); deleteStudent(head, ID); break; case 4: printf("Exiting program...\n"); break; default: printf("Invalid choice. Please try again.\n"); break; } } while(choice != 4); return 0; }
阅读全文