【发布时间】:2019-05-09 18:25:44
【问题描述】:
我写了一个方法,它接受一个文件名,检查文件是否存在并且有内容,然后继续将文件中的 6 个数字读入 6 个int 变量并返回true。如果文件不存在或没有内容,则返回 false。
但是,当我调用该方法并为其提供一个存在的文件时,它返回 false。我不确定我哪里出错了。
这是我的代码:
bool readHSV(string the_file)
{
if(ifstream(the_file)){
fstream myfile(the_file, ios_base::in);
myfile >> h_min >> h_max >> s_min >> s_max >> v_min >> v_max;
cout << h_min << endl << h_max << endl << s_min << endl << s_max
<< endl << v_min << endl << v_max;
getchar();
return true;
}
return false;
}
我正在使用的 .txt 文件的内容:
4
22
68
192
162
247
【问题讨论】:
标签: c++ file text-files ifstream