óÀ½, ÀÌÀü, ´ÙÀ½, ¸¶Áö¸·, Â÷·Ê.
There are two very common special cases of input and output: using files, and using strings in memory.
ÀԷ°ú Ãâ·Â¿¡ ´ëÇÑ µÎ °¡Áö ¸Å¿ì °øÅëÀûÀΠƯº°ÇÑ °æ¿ì°¡ ÀÖ´Ù. ¸Þ¸ð¸®¿¡¼ ÆÄÀÏ°ú ¹®ÀÚ¿(string)À» ÀÌ¿ëÇÏ´Â °ÍÀÌ ±×°ÍÀÌ´Ù.
libio
defines four specialized classes for these cases:
libio
´Â ÀÌ·± °æ¿ì¸¦ À§ÇØ ³× °¡Áö Æ¯ÈµÈ Å¬·¡½º¸¦
Á¤ÀÇÇÑ´Ù.
ifstream
ÆÄÀÏ Àб⸦ À§ÇÑ ¸Þ¼Òµå
ofstream
ÆÄÀÏÀ» ¾²±â À§ÇÑ ¸Þ¼Òµå
istrstream
ostrstream
These methods are declared in `fstream.h'.
ÀÌ ¸Þ¼ÒµåµéÀº 'fstream.h'¿¡¼ ¼±¾ðµÈ´Ù.
You can read data from class ifstream
with any operation from class
istream
. There are also a few specialized facilities:
istream Ŭ·¡½º¿¡¼ ¾î¶² µ¿ÀÛÀ» ¼öÇàÇØ ifstream Ŭ·¡½º·ÎºÎÅÍ µ¥ÀÌÅ͸¦ ÀÐÀ» ¼ö ÀÖ´Ù. ¶ÇÇÑ ¾à°£ÀÇ Æ¯ÈµÈ ±â´ÉµéÀÌ ÀÖ´Ù.
ifstream
associated with a new file for input. (If you
use this version of the constructor, you need to call
ifstream::open
before actually reading anything)
ifstream
À» ¸¸µç´Ù(»ý¼ºÀÚÀÇ ÀÌ ¹öÀüÀ» ÀÌ¿ëÇÑ´Ù¸é ¹«¾ùÀÌµç ½ÇÁ¦·Î Àбâ Àü¿¡ ifstream::open
À» È£ÃâÇؾ߸¸ ÇÑ´Ù).
ifstream
for reading from a file that was already open,
using file descriptor fd. (This constructor is compatible with
other versions of iostreams for POSIX systems, but is not part of
the ANSI working paper.)
ifstream
À» ¸¸µç´Ù(ÀÌ »ý¼ºÀÚ´Â POSIX ½Ã½ºÅÛ iostreamsÀÇ ´Ù¸¥ ¹öÀü°ú
ȣȯµÈ´Ù. ±×·¯³ª ANSI Á¦¾È ¹®¼(working paper)ÀÇ ÀϺκÐÀº ¾Æ´Ï´Ù).
*fname
for this ifstream
object.
ifstream
°´Ã¼¸¦ À§ÇØ *fname
ÆÄÀÏÀ» ¿¬´Ù.
By default, the file is opened for input (with ios::in
as
mode). If you use this constructor, the file will be closed when
the ifstream
is destroyed.
±âº» °ªÀ¸·Î ÆÄÀÏÀº ÀÔ·ÂÀ» À§ÇØ ¿·ÁÁø´Ù(ios::in as
mode·Î). ¸¸¾à ÀÌ »ý¼ºÀÚ¸¦ »ç¿ëÇÑ´Ù¸é ifstream
ÀÌ ÆıâµÉ ¶§ ´ÝÇô¾ß
ÇÒ °ÍÀÌ´Ù.
You can use the optional argument mode to specify how to open the
file, by combining these enumerated values (with `|' bitwise or).
(These values are actually defined in class ios
, so that all
file-related streams may inherit them.) Only some of these modes are
defined in the latest draft ANSI specification; if portability is
important, you may wish to avoid the others.
ÀÓÀÇÀÇ(optional) ÀÎÀÚ mode¸¦ »ç¿ëÇØ ¿°ÅµÈ(enumerated)
°ªÀ» ¿¬°áÇÔÀ¸·Î½á(with `|' bitwise or) ¾î¶»°Ô ÆÄÀÏÀ»
¿ °ÍÀÎÁö ÁöÁ¤ÇÒ ¼ö ÀÖ´Ù(ÀÌ °ªµéÀº ½ÇÁ¦·Î´Â ios
Ŭ·¡½º¿¡ Á¤ÀǵǾî ÀÖ¾î¼ ¸ðµç ÆÄÀÏ °ü·Ã ½ºÆ®¸²ÀÌ ±×°ÍµéÀ» »ó¼ÓÇÒ ¼ö ÀÖ´Ù).
´ÜÁö ÀÌ ¸ðµå Áß ¸î °¡Áö¸¸ °¡Àå ÃÖ±Ù µµ¾È(draft)ÀÎ ANSI
±â¼úÇ¥(specification)¿¡ Á¤ÀǵǾî ÀÖ´Ù.
¸¸¾à À̽ļºÀÌ Áß¿äÇÏ´Ù¸é, ´Ù¸¥ °ÍµéÀ» ÇÇÇϱ⸦ ¿øÇÒÁöµµ ¸ð¸¥´Ù.
ios::in
ios::out
ios::ate
ios::app
ios::trunc
ios::nocreate
ios::noreplace
ios::bin
The last optional argument prot is specific to Unix-like systems; it specifies the file protection (by default `644').
¸¶Áö¸· ÀÓÀÇÀÇ ÀÎÀÚ prot´Â À¯´Ð½º ȣȯ(Unix-like) ½Ã½ºÅÛ¿¡¼ Ưº°ÇÏ´Ù. ±×°ÍÀº ÆÄÀÏ º¸È£(protection)¸¦ ±¸Ã¼ÈÇÑ´Ù(±âº»°ªÀº `644').
ifstream
object
already exists (for instance, after using the default constructor). The
arguments, options and defaults all have the same meanings as in the
fully specified ifstream
constructor.
ifstream
°´Ã¼°¡ È®½ÇÇÏ°Ô ÀÌ¹Ì Á¸ÀçÇϸé ÆÄÀÏÀ» ¿¬´Ù(¿¹¸¦ µé¾î, ±âº» »ý¼ºÀÚ¸¦ ÀÌ¿ëÇÑ ÈÄ). ÀÎÀÚ, ¿É¼Ç, ±âº»°ª ¸ðµÎ ¿ÏÀüÇÏ°Ô ¸í½ÃµÈ(specified) ifstream
»ý¼ºÀÚ¿¡¼ °°Àº Àǹ̴Ù.
You can write data to class ofstream
with any operation from class
ostream
. There are also a few specialized facilities:
ostream
Ŭ·¡½º¿¡¼ ÀÓÀÇÀÇ µ¿ÀÛÀ» ¼öÇàÇØ
ofstream
Ŭ·¡½º¿¡ µ¥ÀÌÅ͸¦ ¾µ ¼ö ÀÖ´Ù. ¶ÇÇÑ ¾à°£ÀÇ Æ¯ÈµÈ ±â´ÉÀÌ ÀÖ´Ù.
ofstream
associated with a new file for output.
ofstream
¸¦ ¸¸µç´Ù.
ofstream
for writing to a file that was already open,
using file descriptor fd.
ofstream
À» ¸¸µç´Ù.
*fname
for this ofstream
object.
ofstream
°´Ã¼¸¦ À§ÇÑ *fname
ÆÄÀÏÀ» ¿¬´Ù.
By default, the file is opened for output (with ios::out
as mode).
You can use the optional argument mode to specify how to open the
file, just as described for ifstream::ifstream
.
±âº»°ªÀ¸·Î Ãâ·ÂÀ» À§ÇØ ÆÄÀÏÀÌ ¿¸°´Ù(ios::out
mode·Î). ÀÓÀÇÀÇ
ÀÎÀÚ mode¸¦ ifstream:ifstream
À» À§ÇÑ ¼³¸íÀ¸·Î½á ¾î¶»°Ô ÆÄÀÏÀ»
¿ °ÍÀÎÁö ÁöÁ¤ÇÑ´Ù.
The last optional argument prot specifies the file protection (by default `644').
¸¶Áö¸· ÀÓÀÇÀÇ ÀÎÀÚ prot´Â ÆÄÀÏ º¸È£¸¦ ¸í½ÃÇÑ´Ù(±âº»°ªÀº `644').
ofstream
objects are closed when the
corresponding object is destroyed.
Ofstream
°´Ã¼¿Í ¿¬°áµÈ ÆÄÀÏÀº ÀÏÄ¡ÇÏ´Â °´Ã¼°¡ ÆıâµÉ
¶§ ´ÝÈù´Ù.
ofstream
object
already exists (for instance, after using the default constructor). The
arguments, options and defaults all have the same meanings as in the
fully specified ofstream
constructor.
ofstream
°´Ã¼°¡ ÀÌ¹Ì È®½ÇÇÏ°Ô Á¸ÀçÇÑ
ÈÄ¿¡ ÆÄÀÏÀ» ¿¬´Ù(¿¹¸¦ µé¾î, ±âº» »ý¼ºÀÚ¸¦ ÀÌ¿ëÇÑ ÈÄ¿¡). ÀÎÀÚ, ¿É¼Ç,
±âº»°ª ¸ðµÎ ¿ÏÀüÇÏ°Ô ¸í½ÃµÈ ofstream
»ý¼ºÀÚ¿¡¼ °°Àº
Àǹ̴Ù.
The class fstream
combines the facilities of ifstream
and
ofstream
, just as iostream
combines istream
and
ostream
.
fstream
Ŭ·¡½º´Â iostream
ÀÌ istream
°ú ostream
À» ¹´Â °Í
ó·³ ifstream
°ú ofstream
ÀÇ ±â´ÉÀ» ¹´Â´Ù.
The class fstreambase
underlies both ifstream
and
ofstream
. They both inherit this additional method:
fstreambase
Ŭ·¡½º´Â ifstream
°ú
ofstream
ÀÇ ±âÃÊ°¡ µÈ´Ù. ±×°ÍµéÀº ÀÌ ºÎ°¡ÀûÀÎ ¸Þ¼Òµå¸¦
»ó¼ÓÇÑ´Ù.
ios::fail
in
this object to mark the event.
ios::fail
¸¦ ¼³Á¤ÇÑ´Ù.
The classes istrstream
, ostrstream
, and strstream
provide some additional features for reading and writing strings in
memory--both static strings, and dynamically allocated strings. The
underlying class strstreambase
provides some features common to
all three; strstreambuf
underlies that in turn.
istrstream
, ostrstream
, strstream
Ŭ·¡½º´Â ¸Þ¸ð¸®¿¡¼ ¹®ÀÚ¿À» ÀÐ°í ¾²±â
À§ÇÑ ¾à°£ÀÇ ºÎ°¡ÀûÀΠƯ¡À» Á¦°øÇÑ´Ù. ÀÌ°ÍÀº Á¤Àû ¹®ÀÚ¿°ú µ¿ÀûÀ¸·Î
ÇÒ´çµÈ ¹®ÀÚ¿À» ¸ðµÎ Æ÷ÇÔÇÑ´Ù.
±â¹ÝÀÎ strstreambase
Ŭ·¡½º´Â ÀϹÝÀûÀ¸·Î ¸ðµÎ ¼¼ °¡Áö Ư¡À» Á¦°øÇÑ´Ù. strstreambuf
´Â
À̹ø¿¡ ±âÃÊ°¡ µÈ´Ù.
istrstream
with an existing
static string starting at str, of size size. If you do not
specify size, the string is treated as a NUL
terminated string.
istrstream
À» Å©±â°¡ sizeÀÎ
str¿¡¼ ½ÃÀÛÇÏ´Â ÀÌ¹Ì Á¸ÀçÇÏ´Â Á¤Àû ¹®ÀÚ¿°ú ¿¬°áÇÑ´Ù. ¸¸¾à size¸¦ ¸í½ÃÇÏÁö
¾ÊÀ¸¸é ¹®ÀÚ¿Àº NUL
·Î ³¡³ª´Â ¹®ÀÚ¿·Î ´Ù·ç¾îÁø´Ù.
ifstream::ifstream
; if you do not specify
one, the new stream is simply open for output, with mode ios::out
.
ifstream::ifstream
·Î
¼³¸íµÈ ¸ðµå Áß Çϳª¸¦ ¼±ÅÃÇØ
¸í½ÃÇÒ ¼ö ÀÖ´Ù; ¸¸¾à ¸í½ÃÇÏÁö ¾ÊÀ¸¸é »õ·Î¿î ½ºÆ®¸²Àº
iso::out
¸ðµå·Î
°£´ÜÇÏ°Ô Ãâ·ÂÀ» À§ÇØ ¿¸°´Ù.
ostrstream
.
ostrstream
°ú ¿¬°áµÈ ¹®ÀÚ¿ÀÇ ÇöÀç ±æÀ̸¦
º¸°í ÇÑ´Ù.
ostrstream
. Implies
`ostrstream::freeze()'.
Ostrstream
¿¡ ÀÇÇØ °ü¸®µÇ´Â ¹®ÀÚ¿ÀÇ Æ÷ÀÎÅÍ. `ostrstream::freeze()'¸¦
¾Ï½ÃÇÑ´Ù.
Note that if you want the string to be nul-terminated, you must do that yourself (perhaps by writing ends to the stream).
¸¸¾à ¹®ÀÚ¿ÀÌ nul-terminatedµÇ±â¸± ¹Ù¶õ´Ù¸é ½º½º·Î ±×°ÍÀ» Çؾ߸¸ ÇÑ´Ù(¾Æ¸¶µµ ½ºÆ®¸²ÀÇ ¾²±â ³¡¿¡).
ostrstream
is not to change dynamically; while frozen,
it will not be reallocated if it needs more space, and it will not be
deallocated when the ostrstream
is destroyed. Use
`freeze(1)' if you refer to the string as a pointer after creating
it via ostrstream
facilities.
ostrstream
°ú ¿¬°áµÈ
¹®ÀÚ¿ÀÌ µ¿ÀûÀ¸·Î ¹Ù²îÁö ¾Ê´Â´Ù´Â °ÍÀ» ¼±¾ðÇ϶ó. Á¦ÇÑµÈ µ¿¾È ´õ ¸¹Àº °ø°£À»
¿ä±¸Çϸé ÀçÇÒ´çµÇÁö ¾Ê´Â´Ù. ±×¸®°í ostrstream
ÀÌ ÆıâµÉ ¶§ deallocatedµÇÁö
¾Ê´Â´Ù. ¸¸¾à¿¡ oststream
±â´ÉµéÀ» ÅëÇØ ±×°ÍÀ» ¸¸µç ÈÄ Æ÷ÀÎÅÍ·Î½á ¹®ÀÚ¿¿¡
µÇµ¹¸®·Á¸é `freeze(1)'¸¦ »ç¿ëÇ϶ó.
`freeze(0)' cancels this declaration, allowing a dynamically
allocated string to be freed when its ostrstream
is destroyed.
`freeze(0)¡¯´Â ÀÌ ¼±¾ðÀ» Ãë¼ÒÇÏ°í ostrstream
ÀÌ
ÆıâµÉ ¶§ freeµÈ µ¿Àû ÇÒ´çµÈ ¹®ÀÚ¿À» Çã¿ëÇÑ´Ù.
If this ostrstream
is already static--that is, if it was created
to manage an existing statically allocated string---freeze
is
unnecessary, and has no effect.
¸¸¾à ÀÌ ostrstream
ÀÌ ÀÌ¹Ì Á¤ÀûÀ̸é, Áï ¸¸¾à
±×°ÍÀÌ Á¸ÀçÇÏ´Â Á¤ÀûÀ¸·Î ÇÒ´çµÈ ¹®ÀÚ¿À» °ü¸®Çϱâ À§ÇØ ¸¸µé¾î Á³´Ù¸é
freeze
´Â ºÒÇÊ¿äÇÏ´Ù. ±×¸®°í È¿°úµµ ¾ø´Ù.
freeze(1)
is in effect for this string.
freeze(1)
¸¦ Å×½ºÆ®ÇÑ´Ù.
strstreambuf
.
strstreambuf Æ÷ÀÎÅÍ