清空文件内容
直接使用文件打开模式
void Analyser::CleanGrades(const string& path){
ofstream ofs;
ofs.open(path, ios::binary | ios::trunc | ios::out);
ofs.close();
}
删除文件中指定字符
C++的流控制只支持读出和写入,但并不支持修改文件,需要通过中间文件的方式截断在流中进行修改
void Analyser::InitGrades_csv(const string& path)
{
fstream in(path, ios::in);//原文件
fstream out(TEMP, ios::out);//中间文件
string line;
while (getline(in, line))//得到test.txt中一行的内容
{
string s;
for (auto c : line) {
if (c == ',') {
s += ' ';
continue;
}
s += c;
}
out << s << "\n";
}
in.close();//关闭流
out.close();
}