From: chea s. o. <y2k...@ya...> - 2003-09-28 06:20:54
|
does anyone know can i use the following code? i want to write something into a binary file. the i will use another to read it? can anyone tell me if i can use the following in dev C++? i picked this example from a book but i can write the file but can't read from the binary file. 2 different files are as below: //this is the write file #include <iostream.h> #include <fstream> #include <stdlib.h> #include <string> using namespace std; struct person { protected : string name; int age ; public: void showdata() { cout<<" name: "<<name<<endl; cout<<" Age: "<<age<<endl; } void getdata() { cout<<"Enter name: ";getline(cin,name); cout<<"Enter Age: ";cin>>age; } }; int main() { person pers; fstream file; file.open("per.txt",ios::app|ios::binary|ios::in|ios::out); pers.getdata(); file.write(reinterpret_cast<char*>(&pers),sizeof(pers)); file.close(); file.seekg(0); system("PAUSE"); return 0; } // this is a read file #include <iostream.h> #include <fstream> #include <stdlib.h> #include <string> using namespace std; struct person { protected : string name; int age ; public: void showdata() { cout<<" name: "<<name<<endl; cout<<" Age: "<<age<<endl; } void getdata() { cout<<"Enter name: ";getline(cin,name); cout<<"Enter Age: ";cin>>age;; } } ; int main() { person pers; fstream file; file.open("per.txt",ios::binary|ios::app|ios::in|ios::out); file.seekg(0); file.read(reinterpret_cast<char*>(&pers),sizeof(pers)); pers.showdata(); file.read(reinterpret_cast<char*>(&pers),sizeof(pers)); system("PAUSE"); return 0; } tell me that u got after compile it with dev C++. Thanks and regard, ygod __________________________________ Do you Yahoo!? The New Yahoo! Shopping - with improved product search http://shopping.yahoo.com |