dsfsu dsfsu - 24 days ago 8
C++ Question

C++ Programming: Asking user if they would like to overwrite a file if it already exists

I am a freshman Computer Science major working on an assignment for my Principles of Programming 1 class. A small part of it involves asking the user if they would like to overwrite a file if it already exists. I am using Code::Blocks and please keep in mind that I am only in my first programming class. Here is the code that I wrote for this:

#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;

int main()
{
char outputFileName[81];
ifstream tempInputFile; // to check if file already exists
char overwrite;
ofstream outputFile;

system( "cls" );
cout << endl << endl;

cout << "Please enter the file for data to be written to: ";
cin >> outputFileName;
cout << endl << endl;
tempInputFile.open( outputFileName );
while ( tempInputFile )
{
cout << "This file already exists. Would you like to overwrite? (Y/N):";
cin >> overwrite;
cout << endl << endl;
if ( overwrite == 'Y' || overwrite == 'y' )
tempInputFile.close();
// I also tried a block if statement here with .clear() before the .close()
else
{
tempInputFile.close();
cout << "Please enter the file for data to be written to: ";
cin >> outputFileName;
cout << endl << endl;
tempInputFile.open( outputFileName );
} // end else
} // end while

tempInputFile.close();
outputFile.open( outputFileName );

cout << "The file is ready to be written to..." << endl << endl;

cout << endl << endl;
system( "pause" );
return 0;
} // end main()


The problem is if an existing file is entered, then the user answers "Y" to overwrite the file, the program will ask again if they want to overwrite the file. Answering "Y" the second time will continue as it should, but I don't understand why it is asking the second time. I did finally write some code that handles it correctly (using a do...while loop), but I want to know why the other code is not doing what I think it should do. Can someone please explain this to me? Here is the code that I wrote that is doing what I want:

#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;

int main()
{
char outputFileName[81];
ifstream tempInputFile;
char overwrite;
ofstream outputFile;

system( "cls" );
cout << endl << endl;

cout << "Please enter the file for data to be written to: ";
cin >> outputFileName;
cout << endl << endl;

tempInputFile.open( outputFileName );
if ( tempInputFile )
do
{
cout << "This file already exists. Would you like to overwrite? (Y/N):";
cin >> overwrite;
cout << endl << endl;
if ( overwrite == 'N' || overwrite == 'n' )
{
tempInputFile.close();
cout << "Please enter the file for data to be written to: ";
cin >> outputFileName;
cout << endl << endl;
tempInputFile.open( outputFileName );
if ( !tempInputFile )
overwrite = 'Y';
} // end if
} while ( overwrite == 'N' || overwrite == 'n' );

cout << "The file is ready to be written to..." << endl << endl;

tempInputFile.close();
outputFile.open( outputFileName );

cout << endl << endl;
system( "pause" );
return 0;
} // end main()

Answer

Let's focus on this part of your code:

tempInputFile.open( outputFileName );
while ( tempInputFile )
{
  cout << "This file already exists. Would you like to overwrite?  (Y/N):";
  cin >> overwrite;
  cout << endl << endl;
  if ( overwrite == 'Y' || overwrite == 'y' )
     tempInputFile.close();       
  else
  {
      // not important
  } // end else
} // end while

when while is called for the first time after successful open you ask user if (s)he wants to overwrite the file and if the answer is Y you close the file and while loop checks condition again.

The problem is that while ( tempInputFile ) expression checks if there are any errors. Since closing a file usualy doesn't cause any problems, the condition is true and you enter while's body again. Then you try to close file again and here is where it fails as file was already closed. So when while ( tempInputFile ) is called for the 3rd time, it quits with false.