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

标签 c++

我是一名计算机科学专业的大一新生,正在为我的编程原理 1 课布置作业。其中一小部分涉及询问用户是否要覆盖已存在的文件。我正在使用 Code::Blocks,请记住我只是在上第一堂编程课。这是我为此编写的代码:

#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()

问题是如果输入一个现有文件,然后用户回答“Y”覆盖该文件,程序将再次询问他们是否要覆盖该文件。第二次回答“Y”会继续,但我不明白为什么要第二次问。我终于写了一些正确处理它的代码(使用 do...while 循环),但我想知道为什么其他代码没有按照我认为应该做的去做。有人可以向我解释一下吗?这是我编写的代码,它正在执行我想要的操作:

#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()

最佳答案

让我们关注这部分代码:

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

while 在成功 open 后第一次被调用时,你询问用户是否要覆盖文件,如果答案是 Y 您关闭文件,while 循环再次检查条件。

问题是 while (tempInputFile) 表达式检查是否有 errors .由于关闭文件通常不会造成任何问题,条件为 true 并且您再次进入 while 的主体。然后您再次尝试关闭文件,但由于文件已经关闭,这里失败了。因此,当第 3 次调用 while ( tempInputFile ) 时,它以 false 退出。

关于C++ 编程 : Asking user if they would like to overwrite a file if it already exists,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40576941/

相关文章:

c++ - 为什么在初始化元素多于数组大小时的数组容器时不会出现错误?

c++ - 错误 22error LNK2005 : _realloc already defined in libcmt. lib(realloc.obj) MSVCRT.lib

用于计算模式出现次数的 C++ 脚本

c++ - 从 QByteArray 解析 Qt5 JSON

c++ - 如果我不正确地重载运算符会报告什么错误?

c++ - 仅替换成员函数的模板语法

c++ - 准备好的语句可以跨线程共享吗?

c++ - CUDA 模板内核和纹理

c++ - 尝试使用取消引用的迭代器进行连接时,无法在字符串 vector 中连接字符串

c++ - 是否有任何优雅的方法来遍历其元素位置可以更改的列表?