943,589 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 14636
  • C++ RSS
Mar 30th, 2007
0

File Handling using C++

Expand Post »
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.

c++ Syntax (Toggle Plain Text)
  1. class Member
  2. {
  3. private:
  4. char name[30];
  5. long int userid;
  6. char depart[30];
  7. int fine;
  8. struct date doj;
  9. char password[8];
  10. public:
  11. Member()
  12. {
  13. strcpy(name,"");
  14. userid=0;
  15. strcpy(depart,"");
  16. fine=0;
  17. struct date d;
  18. getdate(&doj);
  19. strcpy(password,"");
  20. }
  21. long int ret_userid();
  22. char *ret_pass();
  23. void getdata();
  24. void display();
  25. };

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..

c++ Syntax (Toggle Plain Text)
  1. void Member::getdata()
  2. {
  3. cout<<"\Enter the name of the member :";
  4. gets(name);
  5. cout<<"Enter the user-id of the member :";
  6. cin>>userid;
  7. cout<<"Enter the name of the department to which the member belongs to :";
  8. gets(depart);
  9. char *str;
  10. str=getpassword();
  11. int len=strlen(str);
  12. str[len]='\0';
  13. for(int i=0;i<len;i++)
  14. password[i]=str[i];
  15. password[i]='\0';
  16. }
  17. void Member::display()
  18. {
  19. cout<<"\n\nMember Record :=>";
  20. cout<<"\nName of the member :"<<name;
  21. cout<<"\nUser-id :"<<userid;
  22. cout<<"\nDate of joining:"<<doj.da_year<<"/"<<doj.da_mon<<"/"<<doj.da_day;
  23. cout<<"\nDepartment :"<<depart;
  24. cout<<"\nCurrent payable fine :"<<fine;
  25. }
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..
c++ Syntax (Toggle Plain Text)
  1. Member m;
  2. m.getdata();
  3. ofstream fout;
  4. fout.open("member.dat",ios::binary|ios::app);
  5. fout.write((char *)(&m),sizeof(m));
  6. fout.close();
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
crazylunatic is offline Offline
13 posts
since Mar 2007
Mar 30th, 2007
0

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.
Last edited by Ancient Dragon; Mar 30th, 2007 at 6:58 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Mar 30th, 2007
0

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.
The struct date is the same one available in dos.h.And I am aware of the fact that its not a good idea to write classes to files which have other classes or character pointers.But I guess if I am not wrong(I have this sinking feeling that I am wrong!!),I havent used any of the things you mentioned.I had used a character pointer instead of the array for password but I had terrible outputs.So I changed back to an array.Isnt the writing of classes,having character arrays and a structure,to files allowed??
Reputation Points: 10
Solved Threads: 0
Newbie Poster
crazylunatic is offline Offline
13 posts
since Mar 2007
Mar 30th, 2007
0

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Mar 30th, 2007
0

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.
Well here is the date structure:
c++ Syntax (Toggle Plain Text)
  1. struct date {
  2. int da_year; /* current year */
  3. char da_day; /* day of the month */
  4. char da_mon; /* month (1 = Jan) */
  5. };


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..
c++ Syntax (Toggle Plain Text)
  1. printf("%d/%d/%d",doj.da_day,doj.da_mon,doj.da_year);
Last edited by crazylunatic; Mar 30th, 2007 at 7:45 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
crazylunatic is offline Offline
13 posts
since Mar 2007
Oct 2nd, 2010
-2
Re: File Handling using C++
plz send me only file handling project without graphics......
plz help me
thank u
Reputation Points: 10
Solved Threads: 0
Newbie Poster
deeps84 is offline Offline
1 posts
since Oct 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need help writing an algorithm for this program
Next Thread in C++ Forum Timeline: Knights Tour





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC