struct student
{
int identity;
char name[MAX];
int no_assessment;
char assessmenttask[MAX];
int mark;
};
void appendbfile(char filename [MAX])
{
ofstream writeb;
char filenameb [MAX];
strcpy(filenameb,filename);
student s;
strcat(filenameb,".dat");
cout<<"--------------------------------"
<<endl
<<"Begin appending for binary file "
<<filenameb
<<endl
<<endl;
cout<<"Enter student id: ";
cin>>s.identity;
cout<<"Enter student name: ";
cin>>s.name;
writeb.open(strcpy(filenameb,".dat"),ios::binary);
writeb.seekp(0,ios::end);
writeb.write (reinterpret_cast <const char *>(&s), sizeof (s));
writeb.close();
}
I can run the programme but i cant seem to append a record to the binary file. Can someone help me take a look.
Thanks
The problem is in below line, you need to change
to
Because you have done
strcat(filenameb,".dat");already and strcpy insidewriteb.opencopies ‘.dat’ to filenameb which replaced the file name with ‘.dat’. If you look carefully file ‘.dat’ is created in the same directory as your program which has your data.Also as you don’t necessary to call
seekp(0,ios::end);to move file pointer to the end of the file, essentially open file withios::appflag will append file to the end of the file.see file open mode : http://en.cppreference.com/w/cpp/io/ios_base/openmode