ofstream ofs;
ofs.open("d:/test123.txt", ios::out);
ofs << "hello world\n";
ofs << "hello world1\n";
ofs.close();
ifstream ifs;
ifs.open("d:/test123.txt", ios::in);
if (!ifs.is_open()) {
cout << "文件打开失败" << endl;
}
// 读文件
// 1
/*char buf[1024] = {0};
while (ifs >> buf) {
cout << buf << endl;
}*/
// 2
/*char buf[1024] = { 0 };
while (ifs.getline(buf, sizeof(buf))) {
cout << buf << endl;
}*/
// 3
string buf;
while (getline(ifs, buf))
{
cout << buf << endl;
}
ifs.close();