Function to read from a file

I have a function in my library management system that is not working correctly, it is supposed to read student number and book number from library book but it returns student does not exist while the student number is in the file,please help.

void book_issue()
{
char sn,bn;
book bk;
student st;
int found=0,flag=0;
fstream File;
cout<<"\n\nBOOK ISSUE ...";
cout<<"\n\n\tEnter The student's admission no.";
cin>>sn;

File.open("student.dat",ios::in|ios::out);
File.open("book.dat",ios::in|ios::out);
while(File.read((char*)&st,sizeof(student)) && found==0)
{
if((st.retadmno(),sn)==0)
{
found=1;
st.show_student();

cout<<"\n\n\tEnter the book no. ";
cin>>bn;
while(File.read((char*)&bk,sizeof(book))&& flag==0)
{
if((bk.retbno(),bn)==0)
{
bk.show_book();
flag=1;
st.retadmno();
int pos=-1*sizeof(st);
File.seekp(pos,ios::cur);
File.write((char*)&st,sizeof(student));
cout<<"\n\n\t Book issued successfully\n\nPlease Note:";
cout<<"\n\n\t Write current date in backside of book and submit within 15 days fine Rs. 1 for each day after 15 days period";
}
}
if(flag==0){
cout<<"Book no does not exist";
}
else
cout<<"You have not returned the last book ";

}
}
if(found==0)
cout<<"Student record not exist...";
getch();
File.close();

}


Hello imkennedy94,

Welcome to the forum.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

Missing some useful information here. What does the class or struct "student" look like and what does the input file look like?

I am thinking part of the problem the "File.read()" I believe this is best used for reading a binary file and if you are trying to read a block of the input file into something that contains a "std::string" this could be a problem since the string does not have a fixed size.

It is also possible that if you have reached and set "eof" that your stream may neither read or write until the state bits are reset.

If you are reading and writing the ">>" and "<<" should work better than the ".read" that you are using.

I will put something together to see if I can test your code. With out the input that you are using this may be difficult.

Hope that helps,

Andy
Hello imkennedy94,

As I started working with the code I realized that I have no idea what "book" is.

Right now without "book" and "student" I have no way to compile or test the program.

Andy
Topic archived. No new replies allowed.