gpt4 book ai didi

c - 学生数据库管理系统

转载 作者:行者123 更新时间:2023-11-30 21:10:38 25 4
gpt4 key购买 nike

我正在创建一个小型 DBMS,用于记录学生姓名、 Material #、部门和专业。

我使用文本文件作为数据库;我的程序应该添加新学生、搜索学生、删除学生、更新学生信息等。目前,我正在将学生信息添加到数据库中并且工作正常,但我想要的是不断添加学生而无需再次运行程序,直到我按一个键。

所以,我使用了一个循环(同时执行 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);

}


}

最佳答案

您需要关闭文件,一旦打开您可以连续写入,但它不会在您的读取函数上读取新值(因为它作为新文件打开)。阅读后,您可能不需要保持文件打开。使用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);
}


}

关于c - 学生数据库管理系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28977171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com