我正在创建一个小型DBMS,用于记录学生姓名,席位#,部门和专业。
我使用的是文本文件作为我的数据库;我的程序应该添加新学生,搜索学生,删除学生,更新学生信息等。现在,我将学生信息添加到数据库并且工作正常,但我想要的是在不运行程序的情况下不断添加学生直到我按下一个键。
所以,我使用了一个循环(都是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);
}
}
答案 0 :(得分:0)
你需要关闭文件,一旦打开你可以连续写,但它不会读取你读取函数的新值(因为它打开为新的)。阅读后,您可能不需要保持文件打开。使用fclose()
/*
* Files.c
*
* Created on: Mar 2, 2015
* Author: ousainou
*/
# include <stdio.h>
# include <stdlib.h>
void appending();
void readFile();
int main()
{
//printf("do you want to add a student[y/n]");
printf("*********************************\n");
printf("*Students Management System(SMS)*\n");
printf("*********************************\n");
do{
printf("press '1' to add a new student\n");
printf("press '2' to view all students records\n");
printf("press -1 to exit");
//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);
//do{
if(input == 1)
{
appending();
}
else if(input == 2)
{
readFile();
}
else if(input == -1)
{
break;
}
}while(true);
}
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");
fclose(file);
}
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);
fclose(rfile);
}
}