| | |
File Handling using C++
Thread Solved
![]() |
•
•
Join Date: Mar 2007
Posts: 11
Reputation:
Solved Threads: 0
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.
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..
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..
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.
I have this class Member which I am using to store the member details.
c++ Syntax (Toggle Plain Text)
class Member { private: char name[30]; long int userid; char depart[30]; int fine; struct date doj; char password[8]; public: Member() { strcpy(name,""); userid=0; strcpy(depart,""); fine=0; struct date d; getdate(&doj); strcpy(password,""); } long int ret_userid(); char *ret_pass(); void getdata(); void display(); };
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)
void Member::getdata() { cout<<"\Enter the name of the member :"; gets(name); cout<<"Enter the user-id of the member :"; cin>>userid; cout<<"Enter the name of the department to which the member belongs to :"; gets(depart); char *str; str=getpassword(); int len=strlen(str); str[len]='\0'; for(int i=0;i<len;i++) password[i]=str[i]; password[i]='\0'; } void Member::display() { cout<<"\n\nMember Record :=>"; cout<<"\nName of the member :"<<name; cout<<"\nUser-id :"<<userid; cout<<"\nDate of joining:"<<doj.da_year<<"/"<<doj.da_mon<<"/"<<doj.da_day; cout<<"\nDepartment :"<<depart; cout<<"\nCurrent payable fine :"<<fine; }
c++ Syntax (Toggle Plain Text)
Member m; m.getdata(); ofstream fout; fout.open("member.dat",ios::binary|ios::app); fout.write((char *)(&m),sizeof(m)); fout.close();
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.
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.
>>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.
•
•
Join Date: Mar 2007
Posts: 11
Reputation:
Solved Threads: 0
•
•
•
•
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
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.
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.
•
•
Join Date: Mar 2007
Posts: 11
Reputation:
Solved Threads: 0
•
•
•
•
>>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.
c++ Syntax (Toggle Plain Text)
struct date { int da_year; /* current year */ char da_day; /* day of the month */ char da_mon; /* month (1 = Jan) */ };
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)
printf("%d/%d/%d",doj.da_day,doj.da_mon,doj.da_year);
Last edited by crazylunatic; Mar 30th, 2007 at 7:45 pm.
![]() |
Similar Threads
- problems with reading random access line from a file (C++)
- C File handling - search within file without reading content? (C++)
- File Handling in C (C)
- File-based Database Api (C++)
- File Handling Help NEEDED (PHP)
- PHP file handling (PHP)
- problem with convert jumbled text file to unjumbled text file (C)
Other Threads in the C++ Forum
- Previous Thread: database connectivity in c++
- Next Thread: password function (c++)
| Thread Tools | Search this Thread |
api array auto based beginner binary bitmap c++ c/c++ calculator challenge char class classes code coding compile console conversion count createcopyofanyfileinc delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption equation error file forms fstream function functions game garbage givemetehcodez graph gui hmenu homeworkhelp homeworkhelper iamthwee ifstream input insert int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node output parameter pointer primenumbersinrange problem program programming project python random read recursion reference rpg sockets string strings temperature template test text text-file tivoli tree url variable vector video win32 windows winsock wordfrequency wxwidgets






