Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;编程:询问用户是否希望覆盖已存在的文件_C++ - Fatal编程技术网

C++ C++;编程:询问用户是否希望覆盖已存在的文件

C++ C++;编程:询问用户是否希望覆盖已存在的文件,c++,C++,我是计算机科学专业的大一新生,正在为我的编程原理1课做作业。其中一小部分涉及询问用户是否希望覆盖已存在的文件。我使用的是Code::Blocks,请记住我只上过第一节编程课。以下是我为此编写的代码: #include <iostream> #include <cstdlib> #include <fstream> using namespace std; int main() { char outputFileName[81]; ifstrea

我是计算机科学专业的大一新生,正在为我的编程原理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()
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符输出文件名[81];
ifstream temputfile;//检查文件是否已存在
字符覆盖;
流输出文件;
系统(“cls”);

cout在循环的第一部分放置一个
break
语句。虽然您这样做了
temputfile.close()
但就while循环而言,我猜它仍然是一个真值

因此,将其编辑为如下所示:

cin >> overwrite;
cout << endl << endl;
if ( overwrite == 'Y' || overwrite == 'y' )
{
tempInputFile.close();
break;
}
cin>>覆盖;

cout让我们关注代码的这一部分:

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
temputfile.open(outputFileName);
while(temputfile)
{
不能>覆盖;

是的,我现在明白了。非常感谢!
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