![]() |
| ||
| File Handling using C++ I have been doing a small project as part of my lab assignments and I encountered this funny probelm while working with the files in C++. I have this class Member which I am using to store the member details. class Member In this class I am taking the system date and storing it in a date structure as the date of joining.Now here is my getdata function & display function.. void Member::getdata()I am creating an object and call this getdata function and write this to a file.When I am reading it back to some other object from the file and displaying it everything is working fine except date.I am getting year correctly but the day and month are having some garbage value.Here is the code snippet I am using for writing to the file.. Member m;Reading is similar. I thought of many reasons and the only one I can think of was maybe the getdate function gets the system date and copies that into the *doj.And from my small experience I have always had problems with files whenever any kinda dynamic allocation came into picture.I am really bugged and stuck at this point.Please,please help me out asap.Thanks in advance. |
| ||
| Re: File Handling using C++ what does struct date look like? >>fout.write((char *)(&m),sizeof(m)); you really should not write c++ classes to the file like that. classes which contains other c++ classes such as std::string, std::vector, c-pointers, etc, can not be written like that. It is safer and more portable from one run of the application program to another to write out the individual members of the class individually. |
| ||
| Re: File Handling using C++ Quote:
|
| ||
| Re: File Handling using C++ >>The struct date is the same one available in dos.h that file is not supported by most compilers so we don't have any clue what the structure looks like. But I assume the day, month, and year are integers. >>Isnt the writing of classes,having character arrays and a structure,to files allowed?? Yes, of course it is. But you have to be carefule that they do not contain pointers. You might also have to consider the packing factor set by your compiler. Packing is usually not a problem until binary files are read by programs created with different compilers. |
| ||
| Re: File Handling using C++ Quote:
struct date { And my stupid mistake.I didnt see before that the month and date were characters.. So the corrected code was just the cout statement.Replaced it with this.. printf("%d/%d/%d",doj.da_day,doj.da_mon,doj.da_year); |
| All times are GMT -4. The time now is 9:33 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC