【问题标题】:students Database Management System学生数据库管理系统
【发布时间】: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);

                }


    }

【问题讨论】:

    标签: c fopen scanf


    【解决方案1】:

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

    【讨论】:

    • 谢谢兄弟。我已经尝试过了,它的工作原理。你知道我如何通过输入他/她的垫子号来搜索特定的学生
    • 这就是我的工作。使用链表或树等数据结构。一旦运行,程序应该读取文件并将它们放入数据结构中。而且当我插入一个新条目时,它也会放入数据结构中。当我需要搜索某个时,我可以根据我的结构简单地使用搜索方法。或逐行读取文件,拆分行并获取垫号并检查。您可能还需要考虑数据库的大小。
    • 好吧,我建议,先启动它(如你所愿。)。并将其作为一个新问题发布。所以人民将从那里帮助。 (如果您在此处发布链接,我也会注意到它)。如果您满意,请不要忘记接受答案:)。
    • 好的,我会这样做的,我也可以使用数组来做到这一点
    • @ous 也许你可以。首先,想想你会怎么做。然后尝试将其转换为代码。如果不能,请在此处发布。
    猜你喜欢
    • 2013-12-28
    • 2011-12-20
    • 1970-01-01
    • 2019-06-25
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-18
    相关资源
    最近更新 更多