I have a file txt file with the contents

10031James Cameron2346789Spanish TownSt.Catherine12 Clover CrescentDeluxeSinglePassport

I want to read from the file baesd on a seraching fro the id number and returning the datat to a class. But it seems I can't read from the file.

Heres is the code below.

void Cancel()
	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 reserveid=0;
	cout<<"Please enter the guest ID you wish to cancel:";

	guest info1;
	ifstream file ;
	file.open("guest.txt", ios::in) ;
	file.seekg(0,ios::beg) ;
	int found = 0 ;
	while (!file.eof())
		//file.read(reinterpret_cast<char *>(&info1),sizeof(guest));
		guest info1(idnumber,contact,name,street,parish,country,booking,category,idtype);
		if ( reserveid == idnumber )
		found = 1 ;
	file.close() ;



Did you write the file or did someone else and you have to use what you've got? The problem is you need to have some way to separate out fields so you can place the appropriate information in the appropriate variable. The >> operator separates by variable whitespace char or variable type. There are no whitespace char in the line you posted, except the newline char at the end of the line, if there is one. So >> will try to separate by type. In this case it will be able to tell that the first 5 char can be put into an int. But then the sixth char is not a digit so it may put the stream into a failed state, making it unuseable again until the failed state is cleared, or it may try putting the rest of the line in the first string variable presented to it, that is, name and keep going until it finds the first whitespace char to stop it since every char is valid in a string. Some common ways to delineate fields are whitespaces, a default char (commas, bars, semicolons, etc are common delineating characters) and defined field length. If you have the ability to create the file, you should clearly delineate fields.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.