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;
			
		}

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

Edited 6 Years Ago by Ancient Dragon: n/a

This article has been dead for over six months. Start a new discussion instead.