Custom Search

Tuesday, November 4, 2008

Open a file for output but do it carefully.

#include <fstream> //inclusion for file i/o

#include <iostream> //inclusion for cin/cout


 

using namespace std;


 

int main()

{

string outputfile, response;


 

ofstream out_obj;

ifstream in_obj;


 

cout<<"enter the name of your output file:";

cin>> outputfile;


 

in_obj.open(outputfile.c_str());


 

while(!in_obj.fail())

{in_obj.close();

cout<< outputfile<< " already exists\n";

cout << "do you want to overwrite it?\n"<<"Enter (y)es or (n)o:";

cin >> response;

if (response=="y")

{

break;

}

else

{

     cout<<"enter a new name for your output file:";

     cin>> outputfile;

in_obj.open(outputfile.c_str());

}

}


 


 

out_obj.open(outputfile.c_str());


 

cout<<"ok, so now I am writing into "<<outputfile <<" Check it out!\n";

out_obj<<"ok, so this is what I wrote into "<<outputfile<<endl;


 

return 0;

}

No comments: