#include <fstream>
ifstream fin;
fin.open("test.txt");
ofstream fout;
fout.open("test.txt");
fstream fs;
fs.open("test.txt");
fin.open("test.txt", ios_base::in | ios_base::binary);
mode flags
ifstream fin;
// ...
fin.close();
fstream fs;
fs.open("test.txt");
if (fs.is_open())
{
// ...
}
happy
bad
→ 빈 줄이 출력된다.
happy
bad
failbit이 설정됐지만 number에는 여전히 300이 들어 있다.
bad
문자를 읽지 못해 fail 했지만 eof는 아니기 때문에 무한 반복
while (!fin.eof())
{
fin >> number;
if (!fin.fail())
{
cout << number << endl;
continue;
}
if (fin.eof())
{
break;
}
fin.clear();
fin >> trash;
}
fin.close();
ifstream fin("studentRecords.dat", ios_base::in | ios_base::binary);
if (fin.is_open())
{
Record record;
fin.read((char*)&record, sizeof(Record));
}
fin.close();
ofstream fout;
fout.open("test.txt");
string line;
getline(cin, line);
if (!cin.fail())
{
fout << line << endl;
}
fout.close();
// 절대 위치
ios::pos_type pos = fout.tellp() // 쓰기 포인트 위치
ios::pos_type pos = fin.tellg() // 읽기 포인트 위치
fout.seekp(0); // 처음 위치로 이동
// 상대 위치
ios_base::beg // 시작 부터
ios_base::cur // 현재 부터
ios_base::end // 뒤 부터
fout.seekp(20, ios_base::cur); // 현재 위치부터 20 byte 뒤로 이동