【发布时间】:2015-03-11 01:40:39
【问题描述】:
我正在创建一个小型 DBMS,用于记录学生姓名、mat#、部门和专业。
我使用文本文件作为我的数据库;我的程序应该添加新学生、搜索学生、删除学生、更新学生信息等。现在,我正在将学生信息添加到数据库中并且它工作正常,但我想要的是不断添加学生而不再次运行程序直到我按了一个键。
所以,我使用了一个循环(同时执行 while 循环和 while 循环),它工作正常,但我无法将它们附加到数据库中。我不知道出了什么问题。
如果我不使用循环,它会将它附加到数据库中,但如果我使用循环,它不会附加它。
/*
* Files.c
*
* Created on: Mar 2, 2015
* Author: ousainou
*/
# include <stdio.h>
# include <stdlib.h>
void appending();
void readFile();
main()
{
//printf("do you want to add a student[y/n]");
printf("*********************************\n");
printf("*Students Management System(SMS)*\n");
printf("*********************************\n");
printf("press '1' to add a new student\n");
printf("press '2' to view all students records\n");
//frintf("press '3' to view a a student's information");
//printf("press '4' to edit a student's infomation);
//printf("press '5' to delete a student);
int input;
scanf("%d",&input);
int x= 0;
do{
//do{
if(input == 1)
{
appending();
}
else if(input == 2)
{
readFile();
}
printf("press -1 to exit");
scanf("%d",&input);
if(input == -1)
break;
}while(x == 0);
}
int input;
char firstname[20];
char lastname[20];
char mat_number[10];
char department[10];
char major[20];
char read;
int num = 1;
int count = 0;
void appending()
{
FILE*file = fopen("new file.txt","a");
printf("enter first name\n");
scanf("%s", firstname);
printf("enter second name\n");
scanf("%s", lastname );
printf("enter mat number\n");
scanf("%s", mat_number);
printf("enter department\n");
scanf("%s", department);
printf("enter major\n");
scanf("%s", major);
fprintf(file,"\t\t%s\t\t%s\t\t%s\t\t%s\t\t%s\n",firstname,lastname,mat_number,department,major);
if(file)
{
printf("%s %s%s",firstname,lastname,"'s information has been stored\n");
}
else
{
printf("database not found ");
}
}
void readFile()
{
FILE*rfile = fopen("new file.txt","r");
if(!rfile)
{
printf("file not found");
exit(-1);
}
else
{
do{
read = fgetc(rfile);
printf("%c",read);
}while(read != EOF);
}
}
【问题讨论】: