我是一名新生计算机科学专业,主修我的编程原理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()
问题是如果输入现有文件,则用户回答&#34; Y&#34;要覆盖该文件,程序将再次询问是否要覆盖该文件。回答&#34; Y&#34;第二次将继续,但我不明白为什么它第二次问。我最后编写了一些正确处理它的代码(使用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()
答案 0 :(得分:3)
让我们专注于您的代码的这一部分:
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结束。
答案 1 :(得分:1)
在循环的第一部分放置一个break
语句。虽然你做tempinputfile.close()
我的猜测是,就while循环而言,它仍然是一个真正的值。
所以编辑它是这样的:
cin >> overwrite;
cout << endl << endl;
if ( overwrite == 'Y' || overwrite == 'y' )
{
tempInputFile.close();
break;
}
那应该有用!中断将立即退出while循环并允许您覆盖。 不在else
语句下放置一个中断,因为在这种情况下,您希望循环再次执行。
干杯!