I have the following in the txt file

1 Single.Standard Standard Empty ......... ........ 70
2 Single Standard Empty ........ ....... 70

I ahve the code below buts its not deleting the selected id

while(!file.eof())
		{				
			
			file >> roomnumber >> category >> type >> status >> datein >> dateout >> rate ;

			data.setroominfo(roomnumber,category,type,status,datein,dateout,rate);

			if(roomid!=data.getroomnumber())	
			{
				fstream file3;

				file3.open("temp.txt",ios::out|ios::app);
				
				file3 << roomnumber << ' ' << category << ' ' << type << ' ' << status <<  ' ' << datein << ' ' << dateout << ' ' << rate << endl;	
				

				file.close();
				file3.close();
			}
						

			remove("room.txt");
			rename("temp.txt","room.txt");		
				
			cout<<"\nRoom has been removed";

				break;
			
		}

Recommended Answers

All 2 Replies

Don't open the output file for every line you want to write to it. Open the output file before the while loop and keep the file open until everything has been written to it.

line 17: you are closing the input file before it has been read. Move both lines 17 and 18 down after the end of that while loop. The function should look something like this:

ofstream file3("temp.txt"); // open the output file
while( file >> roomnumber >> category >> type >> status >> datein >> dateout >> rate  )
{

   // do stuff here
}
file.close();
file3.close();
// rename the files here

thanks

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.