Jump to content

Remove() problem

- - - - -

  • Please log in to reply
1 reply to this topic

#1
hbk

hbk

    Learning Programmer

  • Members
  • PipPipPip
  • 71 posts
I ahve the code below and I am trying to write the contents to another file then delete that file.The code does compile and creates the temp.txt however it does not delete the guest file.
Thanks for the help in advance.

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

	char reservation[100];

	string reservation1;

	int reserveid=0;

	int found=0;

	int in;

	char ch;


		

	

	cout<<"Do you want to cancel a guest reservation:";


do

{

		cin>>ch;

		if(ch=='n'||ch=='N')

			break;


		cout<<"Please enter the guest ID you wish to cancel:";

		cin>>reserveid;

		


		fstream file;

		file.open( "guest.txt", ios::in | ios::out);


		if ( !file )

			 {

				cerr << "File could not be opened" << endl;

				exit( 1 );

			 } 


		

		guest info1;


		while(!file.eof())

		{				

			

			file >> idnumber >> contact >> name >> street >> parish >> country >> category >> booking >> idtype >> reservation;


			info1.setinfo(idnumber,contact,name,street,parish,country,category,booking,idtype,reservation);


			if(reserveid==info1.getidnumber())	

			{

				info1.display();


				string set="No";


				info1.setreservation(set);

				reservation1=info1.getreservation();

												

				fstream file2;

								

				file2.open("temp.txt",ios::out);

				

					if ( !file2 )

				 {

					cerr << "File could not be opened" << endl;

					exit( 1 );

				 } 

				 

				file2 << idnumber << ' ' << contact << ' ' << name << ' ' << street <<  ' ' << parish << ' ' << country << ' ' << category << ' ' << booking << ' ' << idtype << ' ' << reservation1 << endl;	

				

				file2.close();


						

			}

			

				fstream file3;


				file3.open("temp.txt",ios::out|ios::app);


				while((file >> idnumber >> contact >> name >> street >> parish >> country >> category >> booking >> idtype >> reservation))

				{

					

						info1.setinfo(idnumber,contact,name,street,parish,country,category,booking,idtype,reservation);


						file3 << idnumber << ' ' << contact << ' ' << name << ' ' << street <<  ' ' << parish << ' ' << country << ' ' << category << ' ' << booking << ' ' << idtype << ' ' << reservation << endl;	

				}


					

				

				remove("guest.txt");


				rename("temp.txt","guest.txt");

				

				

				file3.close();

				

				break;

			

		}

		

		file.close();


		

		cout<<"\nDo you want to cancel another reservation:";

		cin >> ch;


		

					

}while(ch=='y' || ch=='Y');		

	

	

	cout<<"\n";

	system("pause");


	

}



#2
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
HI,

What OS you're using

If you're using Windows, then you could use

DeleteFile Details DeleteFile Function (Windows)
Also try _remove();

Also check that you have permission to delete the files.

On Linux,
you could try

system("rm guest.txt");

I hope this helps

Munir




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users