C++์์๋ iostream ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ "cin" ์ด ์ฝ์ ์ ๋ ฅ์, "cout" ์ด ์ฝ์ ์ถ๋ ฅ์ ๋ด๋นํ๋ค.
// ์
๋ ฅ
char[100] name;
std::cin << name;
// ์ถ๋ ฅ
std::cout >> "๋น์ ์ ์ด๋ฆ์" >> name >> "์
๋๋ค." >> std::endl;
๊ทธ๋ ๋ค๋ฉด iostream์ ์ด๋ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ณ , cin๊ณผ cout ์ ์ ํํ ๋ฌด์์ผ๊น?
iostram์ ์คํธ๋ฆผ์ ์ด์ฉํ์ฌ ์ ์ถ๋ ฅ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ๊ฐ์ฒด ์งํฅ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค.
cin๊ณผ cout์ ์คํธ๋ฆผ ํด๋์ค์ ๊ฐ์ฒด์ด๋ค.
๐ข์คํธ๋ฆผ๊ณผ ์คํธ๋ฆผ ๋ฒํผ
์ ๋ ฅ๊ณผ ์ถ๋ ฅ์ด ๋ฐ์ํ๋ฉด ์ ๋ ฅ ๋ฐ์ ํน์ ์ถ๋ ฅํ ๋ฌธ์๋ค์ ์ ์ฅํ๋ค๊ฐ ํ๋ฒ์ ์ฒ๋ฆฌํ๋ ๊ณผ์ ์ ์คํธ๋ฆผ์ด๋ผ๊ณ ํ๋ค.
์คํธ๋ฆผ ๋ฒํผ๋ ์คํธ๋ฆผ ๊ณผ์ ์์ ํ๋ฒ์ ์ฒ๋ฆฌํ ๋ฐ์ดํฐ๋ค์ ์์๋๋ ์ ์ฅ ๊ณต๊ฐ์ด๋ค.
iostream ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์ ์ฒด ๊ฐ์๋๋ ๋ค์๊ณผ ๊ฐ๋ค.
๊ฐ์๋์์ ํ์ธํ ์ ์๋ฏ์ด ๊ฐ ์คํธ๋ฆผ๋ค์ ios_base๋ก๋ถํฐ ํ์๋ ํด๋์ค์ด๊ณ ,
cin , cout , cerr , clog ๋ ์คํธ๋ฆผ ํด๋์ค์ ๊ฐ์ฒด์ด๋ค.
๊ทธ๋ผ ์คํธ๋ฆผ ํด๋์ค์ ์คํธ๋ฆผ ๋ฒํผ์ ๋ํด ์ดํด๋ณด์.
์คํธ๋ฆผ ํด๋์ค
ios_base
ios_base ๋ ์คํธ๋ฆผ ํด๋์ค(istream, ostream, fstream ๋ฑ)์ ๊ธฐ๋ฐ์ด ๋๋ ์ถ์ ํด๋์ค๋ค.
์คํธ๋ฆผ์ ์ํ, ํฌ๋งท ํ๋๊ทธ, ์์ธ ์ฒ๋ฆฌ, ๋ก์ผ์ผ ๋ฑ ์คํธ๋ฆผ ๋์์ ํ์ํ ๊ณตํต ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค.
์ฃผ์ ๊ธฐ๋ฅ
- ์คํธ๋ฆผ ์ํ ๊ด๋ฆฌ
- goodit, eofbit, failbit, badbit ๋ฑ ์คํธ๋ฆผ์ ์ํ๋ฅผ ๋ํ๋ด๋ ๋นํธ ํ๋๊ทธ๋ฅผ ๊ด๋ฆฌํ๋ค.
- rdstate(), clear(), setstate() ๋ฑ์ผ๋ก ์ํ๋ฅผ ํ์ธํ๊ฑฐ๋ ๋ณ๊ฒฝํ ์ ์๋ค.
- ํฌ๋งท ํ๋๊ทธ ๊ด๋ฆฌ
- ์ซ์ ์ถ๋ ฅ ํ์(์ง์, ์์์ , ๋์๋ฌธ์ ๋ฑ)์ ์ ์ดํ๋ ํ๋๊ทธ(dec, hex, oct, showbase, uppercase ๋ฑ)๋ฅผ ์ ๊ณตํ๋ค.
- flags(), setf(), unsetf() ๋ฑ์ผ๋ก ํฌ๋งท ํ๋๊ทธ๋ฅผ ์ค์ /ํด์ ํ ์ ์๋ค.
- ์์ธ ์ฒ๋ฆฌ
- ์คํธ๋ฆผ ์ค๋ฅ ๋ฐ์ ์ ๋์ง ์์ธ์ ์ข ๋ฅ๋ฅผ ์ค์ ํ ์ ์๋ค.
- exceptions()๋ก ์์ธ ๋ง์คํฌ๋ฅผ ์กฐํ/์ค์ ํ๋ค.
- ๋ก์ผ์ผ ๊ด๋ฆฌ
- ์คํธ๋ฆผ์ ์ง์ญํ(์ซ์, ๋ ์ง, ๋ฌธ์ ๋ฑ ์ถ๋ ฅ ํ์)๋ฅผ ๋ด๋นํ๋ค.
- imbue()๋ก ๋ก์ผ์ผ์ ๋ณ๊ฒฝํ ์ ์๋ค.
- ์ฌ์ฉ์ ์ ์ ๋ฐ์ดํฐ ์ ์ฅ
- iword(), pword()๋ฅผ ํตํด ์คํธ๋ฆผ์ ์ฌ์ฉ์ ์ ์ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ ์ ์๋ค.
- ์ฝ๋ฐฑ ๋ฑ๋ก
- ์คํธ๋ฆผ ์ด๋ฒคํธ(erase, imbue, copyfmt)์ ๋ํ ์ฝ๋ฐฑ์ ๋ฑ๋กํ ์ ์๋ค.
ios_base๋ ์ง์ ์ ์ผ๋ก ์ ์ถ๋ ฅ ๊ธฐ๋ฅ์ ์ ๊ณตํ์ง ์์ผ๋ฉฐ, ios๊ฐ ์ด๋ฅผ ์์ํ์ฌ ์ค์ ์คํธ๋ฆผ ํด๋์ค(istream, ostream ๋ฑ)๊ฐ ์ฌ์ฉํ๋ค.
ios
ios ๋ basic_ios<char, char_traits<char>> ์ด๋ค.
using ios = basic_ios<char, char_traits<char>>;
basic_ios ๋ ios_base ๋ฅผ ์์ ๋ฐ์ ์คํธ๋ฆผ์ ๊ณตํต์ ์ธ ์์ฑ์ ํ์ฅํ๋ ํด๋์ค ํ ํ๋ฆฟ์ด๋ค.
extern "C++" template <class _Elem, class _Traits> class basic_ios : public ios_base
- _Elem : ์คํธ๋ฆผ ๋ฒํผ์ ์ ์ฅํ ๋ฐ์ดํฐ (ex: char , wchar_t)
- _Traits : ํด๋น ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃจ๋ ๋ฐฉ๋ฒ (ex: char_traits<char>, char_traits<wchar_t>)
basic_ios ๋ ์คํธ๋ฆผ์ ๊ณตํต์ ์ธ ์ํ์ ๋์์ ๊ด๋ฆฌํ๋ฉฐ, ์ค์ ์ ์ถ๋ ฅ ๊ธฐ๋ฅ์ ์ ๊ณตํ์ง ์๊ณ , ์คํธ๋ฆผ์ ๋ด๋ถ ์ํ์ ํฌ๋งท, ๋ฒํผ, ์ฐ๊ฒฐ(tie) ๋ฑ์ ๋ด๋นํ๋ค.
์ฃผ์ ๊ธฐ๋ฅ
- ์คํธ๋ฆผ ์ํ ๊ด๋ฆฌ
- ์คํธ๋ฆผ์ ์ํ(goodbit, badbit, failbit, eofbit)๋ฅผ ๊ด๋ฆฌํ๋ค.
- clear(), setstate() ๋ฑ์ผ๋ก ์ํ๋ฅผ ์ค์ ํ๊ฑฐ๋ ๋ณ๊ฒฝํ ์ ์๋ค.
- ์คํธ๋ฆผ ๋ฒํผ ๊ด๋ฆฌ
- ์คํธ๋ฆผ์ด ์ฌ์ฉํ๋ ๋ฒํผ(basic_streambuf)๋ฅผ ๊ฐ๋ฆฌํค๋ ํฌ์ธํฐ๋ฅผ ๋ณด์ ํ๋ค.
- rdbuf()๋ก ๋ฒํผ๋ฅผ ์กฐํ/์ค์ ํ ์ ์๋ค.
- ์ฐ๊ฒฐ๋ ์คํธ๋ฆผ(tie) ๊ด๋ฆฌ
- ๋ค๋ฅธ ์ถ๋ ฅ ์คํธ๋ฆผ๊ณผ ์ฐ๊ฒฐํ์ฌ, ์ ๋ ฅ ์ ์ ์๋์ผ๋ก ์ถ๋ ฅ ์คํธ๋ฆผ์ flushํ ์ ์๋ค.
- tie()๋ก ์ฐ๊ฒฐ๋ ์คํธ๋ฆผ์ ์กฐํ/์ค์ ํ๋ค.
- ํฌ๋งท ๋ฌธ์ ๊ด๋ฆฌ
- ์ถ๋ ฅ ์ ์ฌ์ฉ๋๋ ์ฑ์ ๋ฌธ์(fill character)๋ฅผ ๊ด๋ฆฌํ๋ค.
- fill()๋ก ์ฑ์ ๋ฌธ์๋ฅผ ์กฐํ/์ค์ ํ ์ ์๋ค.
- ๋ก์ผ์ผ ๊ด๋ฆฌ
- ์คํธ๋ฆผ์ ์ ์ฉ๋๋ ๋ก์ผ์ผ์ ๊ด๋ฆฌํ๋ค.
- imbue()๋ก ๋ก์ผ์ผ์ ์ค์ ํ ์ ์๋ค.
- ๋ฌธ์ ๋ณํ
- narrow(), widen()์ ํตํด ๋ฌธ์ ํ์ ๋ณํ์ ์ง์ํ๋ค.
์ค์ ์ ์ถ๋ ฅ ํด๋์ค(basic_istream, basic_ostream)๊ฐ basic_ios๋ฅผ ์์ํ์ฌ ๊ตฌํ๋๋ค.
istream
ios ๋ basic_istream<char, char_traits<char>> ์ด๋ค.
using istream = basic_istream<char, char_traits<char>>;
basic_istream ์ basic_ios ๋ฅผ ์์ ๋ฐ์ C++ ํ์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ์์ ์ ๋ ฅ ์คํธ๋ฆผ์ ๋ด๋นํ๋ ํ ํ๋ฆฟ ํด๋์ค๋ค.
cin์ด ์ด ํด๋์ค์ ๊ฐ์ฒด์ด๋ค.
extern "C++" template <class _Elem, class _Traits>
class basic_istream : virtual public basic_ios<_Elem, _Traits>
basic_istream ๋ ํ ์คํธ, ํ์ผ, ๋ฉ๋ชจ๋ฆฌ ๋ฑ ๋ค์ํ ์ ๋ ฅ ์์ค์์ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ด์ค๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค.
std::istream, std::wistream ๋ฑ ํ์ค ์ ๋ ฅ ์คํธ๋ฆผ ํด๋์ค์ ๊ธฐ๋ฐ์ด ๋๋ค.
์ฃผ์ ๊ธฐ๋ฅ
1. ์ ๋ ฅ ์ฐ์ฐ์ ์ ๊ณต
- operator>>๋ฅผ ์ค๋ฒ๋ก๋ํ์ฌ ๋ค์ํ ํ์ (์ ์, ์ค์, ๋ฌธ์์ด ๋ฑ)์ ๋ฐ์ดํฐ๋ฅผ ์คํธ๋ฆผ์์ ์ฝ์ด์ฌ ์ ์๋ค.
basic_istream& __CLR_OR_THIS_CALL operator>>(int& _Val) { // extract an int
static_assert(sizeof(int) == sizeof(long), "Bad overflow assumptions due to sizeof(int) != sizeof(long)");
long _Result = _Val;
_Common_extract_with_num_get(_Result);
_Val = _Result;
return *this;
}
basic_istream& __CLR_OR_THIS_CALL operator>>(unsigned int& _Val) { // extract an unsigned int
return _Common_extract_with_num_get(_Val);
}
basic_istream& __CLR_OR_THIS_CALL operator>>(long& _Val) { // extract a long
return _Common_extract_with_num_get(_Val);
}
basic_istream& __CLR_OR_THIS_CALL operator>>(unsigned long& _Val) { // extract an unsigned long
return _Common_extract_with_num_get(_Val);
}
basic_istream& __CLR_OR_THIS_CALL operator>>(long long& _Val) { // extract a long long
return _Common_extract_with_num_get(_Val);
}
basic_istream& __CLR_OR_THIS_CALL operator>>(unsigned long long& _Val) { // extract an unsigned long long
return _Common_extract_with_num_get(_Val);
}
basic_istream& __CLR_OR_THIS_CALL operator>>(float& _Val) { // extract a float
return _Common_extract_with_num_get(_Val);
}
basic_istream& __CLR_OR_THIS_CALL operator>>(double& _Val) { // extract a double
return _Common_extract_with_num_get(_Val);
}
basic_istream& __CLR_OR_THIS_CALL operator>>(long double& _Val) { // extract a long double
return _Common_extract_with_num_get(_Val);
}
basic_istream& __CLR_OR_THIS_CALL operator>>(void*& _Val) { // extract a void pointer
return _Common_extract_with_num_get(_Val);
}
2. ๋ฒํผ ๊ด๋ฆฌ
- ๋ด๋ถ์ ์ผ๋ก basic_streambuf๋ฅผ ์ฌ์ฉํ์ฌ ์ค์ ๋ฐ์ดํฐ์ ์ ์ถ๋ ฅ์ ๊ด๋ฆฌํ๋ค.
- rdbuf()๋ก ๋ฒํผ์ ์ง์ ์ ๊ทผํ ์ ์๋ค.
3. ์ํ ๊ด๋ฆฌ
- ์ ๋ ฅ ์ฑ๊ณต/์คํจ, EOF ๋ฑ ์คํธ๋ฆผ์ ์ํ๋ฅผ ๊ด๋ฆฌํ๋ค.
- good(), fail(), eof(), bad() ๋ฑ์ผ๋ก ์ํ๋ฅผ ํ์ธํ ์ ์๋ค.
4. ๋ฌธ์ ๋จ์ ์ ๋ ฅ
- get(), getline(), read(), peek(), ignore() ๋ฑ ๋ค์ํ ํจ์๋ก ๋ฌธ์ ๋จ์ ๋๋ ๋ฒํผ ๋จ์๋ก ์ ๋ ฅ์ ๋ฐ์ ์ ์๋ค.
5. ์คํธ๋ฆผ ์์น ์ ์ด
- seekg(), tellg()๋ก ์ ๋ ฅ ์์น๋ฅผ ์ด๋ํ๊ฑฐ๋ ์กฐํํ ์ ์๋ค.
6. ์ ๋ ฅ ์ ์ฒ๋ฆฌ
- sentry๋ผ๋ ๋ด๋ถ ํด๋์ค๋ฅผ ํตํด ์ ๋ ฅ ์ ์คํธ๋ฆผ ์ํ ํ์ธ, ์ฐ๊ฒฐ๋ ์ถ๋ ฅ ์คํธ๋ฆผ flush, ํ์์ ๊ณต๋ฐฑ ๋ฌธ์ ์คํต ๋ฑ์ ์์ ์ ์๋์ผ๋ก ์ฒ๋ฆฌํ๋ค.
Ostream
ostream ์ basic_ostream<char, char_traits<char>> ์ด๋ค.
using ostream = basic_ostream<char, char_traits<char>>;
basic_ostream๋ basic_ios ๋ฅผ ์์ ๋ฐ์ C++ ํ์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ์์ ์ถ๋ ฅ ์คํธ๋ฆผ์ ๋ด๋นํ๋ ํ ํ๋ฆฟ ํด๋์ค์ด๋ค.
extern "C++" template <class _Elem, class _Traits>
class basic_ostream : virtual public basic_ios<_Elem, _Traits>
์ด ํด๋์ค๋ ํ ์คํธ, ํ์ผ, ๋ฉ๋ชจ๋ฆฌ ๋ฑ ๋ค์ํ ์ถ๋ ฅ ๋์์ ๋ฐ์ดํฐ๋ฅผ ๊ธฐ๋กํ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค. std::ostream, std::wostream ๋ฑ ํ์ค ์ถ๋ ฅ ์คํธ๋ฆผ ํด๋์ค์ ๊ธฐ๋ฐ์ด ๋๋ค.
์ฃผ์ ๊ธฐ๋ฅ
- ์ถ๋ ฅ ์ฐ์ฐ์ ์ ๊ณต
- operator<<๋ฅผ ์ค๋ฒ๋ก๋ํ์ฌ ๋ค์ํ ํ์ (์ ์, ์ค์, ๋ฌธ์์ด, ํฌ์ธํฐ ๋ฑ)์ ๋ฐ์ดํฐ๋ฅผ ์คํธ๋ฆผ์ ์ถ๋ ฅํ ์ ์๋ค.
- ๋ฒํผ ๊ด๋ฆฌ
- ๋ด๋ถ์ ์ผ๋ก basic_streambuf๋ฅผ ์ฌ์ฉํ์ฌ ์ค์ ๋ฐ์ดํฐ์ ์ถ๋ ฅ์ ๊ด๋ฆฌํ๋ค.
- rdbuf()๋ก ๋ฒํผ์ ์ง์ ์ ๊ทผํ ์ ์๋ค.
- ์ํ ๊ด๋ฆฌ
- ์ถ๋ ฅ ์ฑ๊ณต/์คํจ ๋ฑ ์คํธ๋ฆผ์ ์ํ๋ฅผ ๊ด๋ฆฌํ๋ค.
- good(), fail(), bad() ๋ฑ์ผ๋ก ์ํ๋ฅผ ํ์ธํ ์ ์๋ค.
- ๋ฌธ์/๋ฐฐ์ด/๋ฒํผ ์ถ๋ ฅ
- put(), write() ๋ฑ์ผ๋ก ๋ฌธ์ ๋๋ ๋ฐฐ์ด์ ์ง์ ์ถ๋ ฅํ ์ ์๋ค.
- ์คํธ๋ฆผ ์์น ์ ์ด
- seekp(), tellp()๋ก ์ถ๋ ฅ ์์น๋ฅผ ์ด๋ํ๊ฑฐ๋ ์กฐํํ ์ ์๋ค.
- ํ๋ฌ์ ๋ฐ ์กฐ์์ ์ง์
- flush(), endl, ends ๋ฑ ์คํธ๋ฆผ์ ํ๋ฌ์ํ๊ฑฐ๋ ํน์ ๋ฌธ์๋ฅผ ์ถ๋ ฅํ๋ ์กฐ์์๋ฅผ ์ง์ํ๋ค.
- ์ถ๋ ฅ ์ ์ฒ๋ฆฌ
- sentry๋ผ๋ ๋ด๋ถ ํด๋์ค๋ฅผ ํตํด ์ถ๋ ฅ ์ ์คํธ๋ฆผ ์ํ ํ์ธ, ์ฐ๊ฒฐ๋ ์ ๋ ฅ ์คํธ๋ฆผ flush, ํ์์ ๋ฝ(lock) ๋ฑ์ ์์ ์ ์๋์ผ๋ก ์ฒ๋ฆฌํ๋ค.
์คํธ๋ฆผ ๋ฒํผ
streambuf
strambuf๋ basic_strambuf<char,char_traits<char>> ์ด๋ค.
using streambuf = basic_streambuf<char, char_traits<char>>;
basic_streambuf๋ C++ ํ์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ์์ ์คํธ๋ฆผ์ ๋ด๋ถ ๋ฒํผ(์ ์ถ๋ ฅ ๋ฒํผ)๋ฅผ ๊ด๋ฆฌํ๋ ํ ํ๋ฆฟ ๊ธฐ๋ฐ ํด๋์ค๋ค.
_EXPORT_STD extern "C++" template <class _Elem, class _Traits>
class basic_streambuf
๋ชจ๋ ์คํธ๋ฆผ(istream, ostream, fstream ๋ฑ)์ ์ค์ ๋ฐ์ดํฐ์ ์ ์ถ๋ ฅ ์ฒ๋ฆฌ๋ฅผ ์ํด basic_streambuf๋ฅผ ์ฌ์ฉํ๋ค.
#include <fstream>
std::ifstream fin("data.txt");
std::streambuf* buf = fin.rdbuf(); // ํ์ผ ์
๋ ฅ ์คํธ๋ฆผ์ ๋ด๋ถ ๋ฒํผ์ ์ ๊ทผ
์ฃผ์ ๊ธฐ๋ฅ
- ์
์ถ๋ ฅ ๋ฒํผ ๊ด๋ฆฌ
- ์ ๋ ฅ ๋ฒํผ(์ฝ๊ธฐ)์ ์ถ๋ ฅ ๋ฒํผ(์ฐ๊ธฐ)๋ฅผ ์ง์ ๊ด๋ฆฌํ๋ค.
- ๋ฒํผ์ ํฌ์ธํฐ์ ํฌ๊ธฐ๋ฅผ ์ค์ ํ๊ณ , ํ์ฌ ์์น๋ฅผ ์ถ์ ํ๋ค.
- ๋ฒํผ๋ฅผ ํตํ ๋ฐ์ดํฐ ์
์ถ๋ ฅ
- sgetc(), sbumpc(), sputc(), sgetn(), sputn() ๋ฑ ๋ค์ํ ํจ์๋ก ๋ฒํผ์์ ๋ฐ์ดํฐ๋ฅผ ์ฝ๊ฑฐ๋ ์ด๋ค.
- ๋ฒํผ๊ฐ ๋น์๊ฑฐ๋ ๊ฐ๋ ์ฐผ์ ๋ underflow(), overflow() ๋ฑ์ ๊ฐ์ ํจ์๊ฐ ํธ์ถ๋์ด ์ค์ ์ ์ถ๋ ฅ ๋์์ ์ํํ๋ค.
- ์คํธ๋ฆผ ์์น ์ ์ด
- pubseekoff(), pubseekpos() ๋ฑ์ผ๋ก ์คํธ๋ฆผ์ ์ฝ๊ธฐ/์ฐ๊ธฐ ์์น๋ฅผ ์ด๋ํ ์ ์๋ค.
- ๋๊ธฐํ ๋ฐ ๋ฒํผ ์ ์ด
- pubsync()๋ก ์ธ๋ถ ์ฅ์น(ํ์ผ, ๋คํธ์ํฌ ๋ฑ)์ ๋ฒํผ๋ฅผ ๋๊ธฐํํ๋ค.
- setbuf()๋ก ์ฌ์ฉ์ ์ ์ ๋ฒํผ๋ฅผ ์ค์ ํ ์ ์๋ค.
- ๋ก์ผ์ผ ๊ด๋ฆฌ
- ์คํธ๋ฆผ์ ์ ์ฉ๋๋ ๋ก์ผ์ผ์ ์ ์ฅํ๊ณ , ๋ณ๊ฒฝํ ์ ์๋ค.
- ํ์ ํด๋์ค์ ๊ธฐ๋ฐ
- ์ค์ ์ ์ถ๋ ฅ ์ฅ์น(ํ์ผ, ๋ฉ๋ชจ๋ฆฌ, ๋คํธ์ํฌ ๋ฑ)์ ๋ง๋ ๋ฒํผ ๋์์ ์ํด ํ์ ํด๋์ค(basic_filebuf, basic_stringbuf ๋ฑ)๊ฐ basic_streambuf๋ฅผ ์์๋ฐ์ ๊ตฌํํ๋ค.
filebuf
filebuf๋ basic_filebuf<char, char_traits<char>> ์ด๋ค.
using filebuf = basic_filebuf<char, char_traits<char>>;
basic_filebuf๋ C++ ํ์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ์์ ํ์ผ๊ณผ ์คํธ๋ฆผ์ ์ฐ๊ฒฐํ๋ ๋ฒํผ ์ญํ ์ ํ๋ ํ ํ๋ฆฟ ํด๋์ค๋ค. ํ์ผ ์ ์ถ๋ ฅ ์คํธ๋ฆผ(ifstream, ofstream, fstream)์ ๋ด๋ถ์์ ์ค์ ํ์ผ๊ณผ์ ๋ฐ์ดํฐ ๊ตํ์ ๋ด๋นํ๋ค. basic_streambuf๋ฅผ ์์๋ฐ์ ํ์ผ์ ํนํ๋ ๋ฒํผ ๋์์ ๊ตฌํํฉ๋๋ค.
_EXPORT_STD template <class _Elem, class _Traits>
class basic_filebuf : public basic_streambuf<_Elem, _Traits>
์ฃผ์ ๊ธฐ๋ฅ
- ํ์ผ๊ณผ ์คํธ๋ฆผ ์ฐ๊ฒฐ
- C ํ์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ FILE* ํฌ์ธํฐ๋ฅผ ์ฌ์ฉํ์ฌ ํ์ผ์ ์ด๊ณ ๋ซ์ผ๋ฉฐ, ์คํธ๋ฆผ๊ณผ ํ์ผ์ ์ฐ๊ฒฐํ๋ค.
- open(), close(), is_open() ๋ฑ์ผ๋ก ํ์ผ์ ์ด๋ฆผ/๋ซํ ์ํ๋ฅผ ๊ด๋ฆฌํ๋ค.
- ์
์ถ๋ ฅ ๋ฒํผ ๊ด๋ฆฌ
- ํ์ผ์์ ๋ฐ์ดํฐ๋ฅผ ์ฝ๊ฑฐ๋ ์ธ ๋ ๋ด๋ถ ๋ฒํผ๋ฅผ ์ฌ์ฉํ์ฌ ํจ์จ์ ์ผ๋ก ์ฒ๋ฆฌํ๋ค.
- xsgetn(), xsputn() ๋ฑ์ผ๋ก ๋ฒํผ ๋จ์ ์ ์ถ๋ ฅ์ ์ง์ํ๋ค.
- ํ์ผ ์์น ์ ์ด
- seekoff(), seekpos()๋ก ํ์ผ ๋ด ์ฝ๊ธฐ/์ฐ๊ธฐ ์์น๋ฅผ ์ด๋ํ ์ ์๋ค.
- ๋ฌธ์ ๋ณํ ์ง์
- ํ ์คํธ ํ์ผ์ ์ธ์ฝ๋ฉ ๋ณํ์ ์ํด codecvt facet์ ํ์ฉํ ์ ์๋ค(์: UTF-8 ↔ UTF-16).
- ๋ฒํผ ๋๊ธฐํ ๋ฐ ์ค์
- sync()๋ก ํ์ผ๊ณผ ๋ฒํผ๋ฅผ ๋๊ธฐํํ๋ค.
- setbuf()๋ก ์ฌ์ฉ์ ์ ์ ๋ฒํผ๋ฅผ ์ค์ ํ ์ ์๋ค.
- ํ์ ์คํธ๋ฆผ ํด๋์ค์ ๊ธฐ๋ฐ
- ifstream, ofstream, fstream ๋ฑ ํ์ผ ์คํธ๋ฆผ ํด๋์ค์์ ๋ด๋ถ์ ์ผ๋ก ์ฌ์ฉ๋๋ค.