【发布时间】:2016-07-09 19:02:46
【问题描述】:
远未完成,但现在我正试图让这个程序询问文件名并将其存储在字符串中,然后转换为 ifstream,然后通过调用单独的函数 isValid 来检查文件是否有效,如果它会返回 true如果不是有效,则为假,如果有效,则主函数将输出“文件有效”。然后它会一直重复这个直到进入退出。但它每次都返回false,我不知道出了什么问题。我将竭诚为您提供帮助。
# include <iostream>
#include <string>
#include<fstream>
using namespace std;
bool isValid(ifstream& file)
{
if (file.good())
{
return true;
}
else
{
return false;
}
}
int main()
{
string file_name;
cout <<"please enter a HTML file name or hit 'exit' to quit and if you want to clear file please enter 'clear': ";
cin >> file_name;
ifstream my_file(file_name.c_str());
while (file_name != "exit")
{
if ((isValid(my_file)) == true)
{
cout << "Hello" << endl;
}
string file_name;
cout <<"please enter a HTML file name or hit 'exit' to quit and if you want to clear file please enter 'clear': ";
cin >> file_name;
ifstream my_file(file_name.c_str());
}
}
【问题讨论】:
标签: c++ file validation loops boolean