Why is my Library Management program not writing to file

Hi,
I am creating a library Management which you can add books, dvds and students to. The books and dvds can then be issued to the Students. The books, dvds and students are written to a .dat file.

The problem I am having is that the program doesn't seem to be writing the students to the .dat file and therefore when I search for a student or try to issue a book student it doesn't find the student, it gives me an error saying student not found. Can someone tell me what I am doing wrong.

I have included some of the code below.

Library.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
 void Library::insertStudent()
    {
        std::fstream file;
        file.open("student.dat",std::ios::out|std::ios::app);
        student1.newStudent();
        file.write((char*)&student1,sizeof(Student));
        file.close();
    }
    
    void Library::searchStudent(char regno[])
    {
        std::fstream file;
        int flag=0;
        file.open("student.dat",std::ios::in);
        while(file.read((char*)&student1,sizeof(Student)))
        {
            if((strcmp(student1.retregistrationNo(),regno)==0))
            {
                student1.displayStudent();
                flag=1;
            }
        }
        
        file.close();
        if(flag==0)
            std::cout<<"Error: Student not found in the system. \n";
    }
    
    void Library::updateStudent()
    {
        std::fstream file;
        char regno[6];
        int found=0;
        std::cout<<"Enter Student Registration no. \n";
        std::cin>>regno;
        file.open("student.dat",std::ios::in|std::ios::out);
        while(file.read((char*)&student1,sizeof(Student)) && found==0)
        {
            if(strcmp(student1.retregistrationNo(),regno)==0)
            {
                student1.displayStudent();
                std::cout<<"Enter student's new details\n";
                student1.editStudent();
                long pos=-1*sizeof(student1);
                file.seekp(pos,std::ios::cur);
                file.write((char*)&student1,sizeof(Student));
                std::cout<<"Student's details Updated\n";
                found=1;
            }
        }
        
        file.close();
        if(found==0)
            std::cout<<"Error: Student not found in the system. \n";
        
    }
    
    void Library::deleteStudent()
    {
        std::fstream file, file2;
        char n[6];
        int flag=0;
        
        std::cout<<"Enter The registration no. of the Student You Want To Delete : \n";
        std::cin>>n;
        file.open("student.dat",std::ios::in|std::ios::out);
        file2.open("Temp.dat",std::ios::out);
        file.seekg(0,std::ios::beg);
        while(file.read((char*)&student1,sizeof(Student)))
        {
            if(strcmp(student1.retregistrationNo(),n)!=0)
                file2.write((char*)&student1,sizeof(Student));
            else
                flag=1;
        }
        
        file2.close();
        file.close();
        remove("student.dat");
        rename("Temp.dat","student.dat");
        if(flag==1)
            std::cout<<"Student deleted from the system. \n";
        else
            std::cout<<"Error: Student not found in the system. \n";
    }
    
    void Library::showallStudents()
    {
        std::fstream file;
        file.open("student.dat",std::ios::in);
        if(!file)
        {
            std::cout<<"Error: File could not be opened. \n";
            
            return;
        }
        
        while(file.read((char*)&student1,sizeof(Student)))
        {
            student1.report();
            break;
        }
        
        file.close();
        
    }
Last edited on
Does it work in binary mode?
What do you mean by binary mode?
You have a number of problems, I'll deal with them first rather that work with annoying code.


This may be source of your error, but you need to understand (and probably explain) it.

A file can be written as "text" where you get a bunch of characters separated by lines. Or you can pack them into bytes in the file, we call this a binary file and such a file cannot be viewed in a text editor (see that word text again?).

File operations in text mode open a stream and call << operators

File operations in binary mode open the file in binary mode (on Windows) and call read/write.

You can't mix them. From your code, you're writing binary files. Don't do that. Write text files. That way you can look at the files and see if they're correct.


That read/write Student business. What is Student? If it has objects, you're in trouble.


Why are you using char arrays for strings when there's a string class?


Code like:
1
2
        std::fstream file;
        file.open("student.dat",std::ios::out|std::ios::app);

should be:
 
        std::ofstream file("student.dat", std::ios::app);

And don't call close on a stream, let the destructor close it for you, that's why it's there.


A word of warning, never use C casts in C++. And when you cast in C++, think really hard if what you're doing is alright; you're overriding the type system and taking full responsibility for the consequences, if you get it wrong you'll crash your program.
Topic archived. No new replies allowed.