// ios::in 与 ios::out 很好区分
// 以人为主体,in——向内输入,即读文件操作;out——向外输出,即写文件操作。
// ios::trunc 覆盖(删除)原文件所有内容
// ios::in
ifstream ifs;
ifs.open(COMPUTER_FILE, ios::in);
ComputerRoom c;
while (ifs >> c.m_ComId && ifs >> c.m_MaxNum)
{
vCom.push_back(c);
}
cout << "数量为: " << vCom.size() << endl;
ifs.close();
// ios::out
ofstream ofs;
ofs.open(fileName, ios::out);
string name;
cout << "请输入姓名: " << endl;
cin >> name;
ofs << name << endl;
cout << "添加成功" << endl;
system("pause");
system("cls");
ofs.close();
// ios::trunc
void cleanFile()
{
ofstream ofs(filename, ios::trunc);
ofs.close();
cout << "清空成功!" << endl;
system("pause");
system("cls");
}
C++中ios::in, ios::out, ios::trunc辨析
最新推荐文章于 2025-04-06 09:23:06 发布