File Handling using C++

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2007
Posts: 11
Reputation: crazylunatic is an unknown quantity at this point 
Solved Threads: 0
crazylunatic crazylunatic is offline Offline
Newbie Poster

File Handling using C++

 
0
  #1
Mar 30th, 2007
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.

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

  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..
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,362
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: File Handling using C++

 
0
  #2
Mar 30th, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 11
Reputation: crazylunatic is an unknown quantity at this point 
Solved Threads: 0
crazylunatic crazylunatic is offline Offline
Newbie Poster

Re: File Handling using C++

 
0
  #3
Mar 30th, 2007
Originally Posted by Ancient Dragon View Post
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??
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,362
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: File Handling using C++

 
0
  #4
Mar 30th, 2007
>>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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 11
Reputation: crazylunatic is an unknown quantity at this point 
Solved Threads: 0
crazylunatic crazylunatic is offline Offline
Newbie Poster

Re: File Handling using C++

 
0
  #5
Mar 30th, 2007
Originally Posted by Ancient Dragon View Post
>>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:
  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..
  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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC