Jump to content

Writing an object to file{help}

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
hbk

hbk

    Learning Programmer

  • Members
  • PipPipPip
  • 71 posts
I ahve the below code but the problem is that it creates the file but it is empty


void New()

{

	

	int idnumber;

	int contact;

	char name[100];

	char country[100];

	char street[100];

	char parish[100];

	char category[100];

	char booking[100];

	char idtype[100];

	int lnumber;

	char make[100];

	char model[100];

	char color[100];

	char type[100];

	char ans;

	char ch[1];

	


	

	

			cout<<"Please enter the guest information below";

					

			

					fflush(stdin);


					cout<<"\nEnter name:";

					cin.getline(name,100);

					

			

					fflush(stdin);

					cout<<"Enter idnumber:";

					cin>>idnumber;

					

					fflush(stdin);

					cout<<"Enter contact:";

					cin>>contact;

			


					fflush(stdin);

					cout<<"Enter address line 1:";

					cin.getline(street,100);


					fflush(stdin);

					cout<<"Enter address line 2:";

					cin.getline(parish,100);


					fflush(stdin);

					cout<<"Enter address line 3:";

					cin.getline(country,100);


					fflush(stdin);

					cout<<"Enter category:";

					cin.getline(category,100);


					fflush(stdin);

					cout<<"Enter booking:";

					cin.getline(booking,100);


					fflush(stdin);

					cout<<"Enter idtype:";

					cin.getline(idtype,100);


					

					cout<<"Does the customer have a vechile:";

					cin>>&ans;


					if(ans=='y'||ans=='Y')

					{

						fflush(stdin);

						cout<<"\nEnter license number:";

						cin>>lnumber;


						fflush(stdin);

						cout<<"Enter make:";

						cin.getline(make,100);


						fflush(stdin);

						cout<<"Enter model:";

						cin.getline(model,100);


						fflush(stdin);

						cout<<"Enter color:";

						cin.getline(color,100);


						fflush(stdin);

						cout<<"Enter type:";

						cin.getline(type,100);	

						

						vehicle now(lnumber,make ,model,color,type);

				

						now.display();

						

					}


				guest info(idnumber,contact,name,street,parish,country,booking,category,idtype);

	


				info.display();

						

				

	

				

				ofstream file("guest.txt",ios::out);

				file.open(reinterpret_cast<char *>(&guest),sizeof(info);							

				file.close();


	

	cout<<"\n";

	system("pause");


}



#2
Infinity

Infinity

    Newbie

  • Members
  • PipPip
  • 24 posts
Try replace this

ofstream file("guest.txt",ios::out);



ofstream file ("guest.bin", ios::out | ios::app | ios::binary);

And see this: Input/Output with files

Infinity

Edited by Infinity, 03 January 2011 - 09:06 AM.