从CSV文件中读取数据代码:
bool speechManager::fileIsEmpty()
{
//创建文件流
ofstream ifs(FILENAME, ios::in);
//判断文件是否打开成功
if(!ifs.is_open())
{
cout << "open false" << endl;
return false;
}
char ch;
ifs >> ch;
if(ifs.eof())
{
//文件为空
cout << "no records" << endl;
ifs.close();
return false;
}
//读取记录
ifs.seekg(0, std::ios::beg);
int count = 0; //记录总共记录有多少条
vector<string> vec; //按行读取每一届记录,有几届就有几个string
string str; //每一行读取的数据
while(getline(ifs, str))
{
vec.push_back(str);
count++;
}
//关闭文件
ifs.close();
return true;
}
运行报错:
error: no match for 'operator>>' (operand types are 'std::ofstream {aka std::basic_ofstream<char>}' and 'char') ifs >> ch;
分析和解决:
上面的代码是用ofstream打开文件,即写入的方式,但是后面却用它来读取文件ifs >> ch,将ofstream换成输入流ifstream即可