óÀ½, ÀÌÀü, ´ÙÀ½, ¸¶Áö¸·, Â÷·Ê.


Classes for Files and Strings

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
Methods for reading files.

ÆÄÀÏ Àб⸦ À§ÇÑ ¸Þ¼Òµå

ofstream
Methods for writing files.

ÆÄÀÏÀ» ¾²±â À§ÇÑ ¸Þ¼Òµå

istrstream
Methods for reading strings from memory.
¸Þ¸ð¸®¿¡¼­ ¹®ÀÚ¿­À» Àбâ À§ÇÑ ¸Þ¼Òµå
ostrstream
Methods for writing strings in memory.
¸Þ¸ð¸®¿¡ ¹®ÀÚ¿­À» ¾²±â À§ÇÑ ¸Þ¼Òµå

Reading and writing files

ÆÄÀÏ ÀÐ°í ¾²±â

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 Ŭ·¡½º·ÎºÎÅÍ µ¥ÀÌÅ͸¦ ÀÐÀ» ¼ö ÀÖ´Ù. ¶ÇÇÑ ¾à°£ÀÇ Æ¯È­µÈ ±â´ÉµéÀÌ ÀÖ´Ù.

Constructor: ifstream::ifstream ()
Make an 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À» È£ÃâÇؾ߸¸ ÇÑ´Ù).
 

Constructor: ifstream::ifstream (int fd)
Make an 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.)
ÆÄÀÏ ¼³¸íÀÚ fd¸¦ ÀÌ¿ëÇØ ÀÌ¹Ì ¿­¸° ÆÄÀϷκÎÅÍ Àбâ À§ÇØ ifstreamÀ» ¸¸µç´Ù(ÀÌ »ý¼ºÀÚ´Â POSIX ½Ã½ºÅÛ iostreamsÀÇ ´Ù¸¥ ¹öÀü°ú ȣȯµÈ´Ù. ±×·¯³ª ANSI Á¦¾È ¹®¼­(working paper)ÀÇ ÀϺκÐÀº ¾Æ´Ï´Ù).
 

Constructor: ifstream::ifstream (const char* fname [, int mode [, int prot]])
Open a file *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
Open for input. (Included in ANSI draft.)
ÀÔ·ÂÀ» ¿¬´Ù(ANSI µµ¾È¿¡ Æ÷ÇÔµÊ).
ios::out
Open for output. (Included in ANSI draft.)
Ãâ·ÂÀ» ¿¬´Ù(ANSI µµ¾È¿¡ Æ÷ÇÔµÊ).
ios::ate
Set the initial input (or output) position to the end of the file ÆÄÀÏÀÇ ³¡¿¡ ÀÔ·Â(¶Ç´Â Ãâ·Â)À» ÃʱâÈ­ ¼³Á¤
ios::app
Seek to end of file before each write. (Included in ANSI draft.)
¾²±â Àü¿¡ ÆÄÀÏÀÇ ³¡À» ã´Â´Ù(ANSI µµ¾È¿¡ Æ÷ÇÔµÊ).
ios::trunc
Guarantee a fresh file; discard any contents that were previously associated with it.
»ý»ýÇÑ(fresh ^^) ÆÄÀÏÀÓÀ» º¸ÀåÇÑ´Ù. ÀÌÀü¿¡ ¿¬°áµÈ ¸ðµç ³»¿ëÀ» ¹ö¸°´Ù.
ios::nocreate
Guarantee an existing file; fail if the specified file did not already exist.
Á¸ÀçÇÏ´Â ÆÄÀÏÀÓÀ» º¸ÀåÇÑ´Ù. ÀÌ¹Ì Á¸ÀçÇϴ Ưº°ÇÑ ÆÄÀÏÀÌ ¾Æ´Ï¸é ½ÇÆÐ
ios::noreplace
Guarantee a new file; fail if the specified file already existed.
»õ·Î¿î ÆÄÀÏÀÓÀ» º¸ÀåÇÑ´Ù. Ưº°ÇÑ ÆÄÀÏÀÌ ÀÌ¹Ì Á¸ÀçÇÏ¸é ½ÇÆÐ
ios::bin
Open as a binary file (on systems where binary and text files have different properties, typically how `\n' is mapped; included in ANSI draft).
¹ÙÀ̳ʸ® ÆÄÀÏ·Î ¿¬´Ù(ÀϹÝÀûÀ¸·Î `\n'Àº ¾î¶»°Ô »ç»ó(map)µÇ¾ú´ÂÁö ½Ã½ºÅÛ¿¡¼­ ¹ÙÀ̳ʸ®¿Í ÅؽºÆ® ÆÄÀÏÀº ´Ù¸¥ ÇÁ·ÎÆÛƼ¸¦ °®´Â´Ù. ANSI µµ¾È¿¡ Æ÷ÇÔµÊ)

The last optional argument prot is specific to Unix-like systems; it specifies the file protection (by default `644').

¸¶Áö¸· ÀÓÀÇÀÇ ÀÎÀÚ prot´Â À¯´Ð½º ȣȯ(Unix-like) ½Ã½ºÅÛ¿¡¼­ Ưº°ÇÏ´Ù. ±×°ÍÀº ÆÄÀÏ º¸È£(protection)¸¦ ±¸Ã¼È­ÇÑ´Ù(±âº»°ªÀº `644').

Method: void ifstream::open (const char* fname [, int mode [, int prot]])
Open a file explicitly after the associated 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 Ŭ·¡½º¿¡ µ¥ÀÌÅ͸¦ ¾µ ¼ö ÀÖ´Ù. ¶ÇÇÑ ¾à°£ÀÇ Æ¯È­µÈ ±â´ÉÀÌ ÀÖ´Ù.

Constructor: ofstream::ofstream ()
Make an ofstream associated with a new file for output.
Ãâ·ÂÀ» À§ÇÑ »õ·Î¿î ÆÄÀÏ°ú ¿¬°áµÈ ofstream¸¦ ¸¸µç´Ù.

Constructor: ofstream::ofstream (int fd)
Make an ofstream for writing to a file that was already open, using file descriptor fd.
ÆÄÀÏ ¼³¸íÀÚ fd¸¦ ÀÌ¿ëÇÏ¿© ÀÌ¹Ì ¿­·ÁÁø ÆÄÀÏ¿¡ ¾²±â À§ÇÑ ofstreamÀ» ¸¸µç´Ù.

Constructor: ofstream::ofstream (const char* fname [, int mode [, int prot]])
Open a file *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').

 

Destructor: ofstream::~ofstream ()
The files associated with ofstream objects are closed when the corresponding object is destroyed.
Ofstream °´Ã¼¿Í ¿¬°áµÈ ÆÄÀÏÀº ÀÏÄ¡ÇÏ´Â °´Ã¼°¡ ÆıâµÉ ¶§ ´ÝÈù´Ù.
 

Method: void ofstream::open (const char* fname [, int mode [, int prot]])
Open a file explicitly after the associated 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ÀÇ ±âÃÊ°¡ µÈ´Ù. ±×°ÍµéÀº ÀÌ ºÎ°¡ÀûÀÎ ¸Þ¼Òµå¸¦ »ó¼ÓÇÑ´Ù.

Method: void fstreambase::close ()
Close the file associated with this object, and set ios::fail in this object to mark the event.
ÀÌ °´Ã¼¿Í ¿¬°áµÈ ÆÄÀÏÀ» ´Ý°í À̺¥Æ®¸¦ Ç¥½ÃÇϱâ À§ÇØ ios::fail¸¦ ¼³Á¤ÇÑ´Ù.

Reading and writing in memory

¸Þ¸ð¸®¿¡¼­ ÀÐ°í ¾²±â

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´Â À̹ø¿¡ ±âÃÊ°¡ µÈ´Ù.

Constructor: istrstream::istrstream (const char* str [, int size])
Associate the new input string class 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·Î ³¡³ª´Â ¹®ÀÚ¿­·Î ´Ù·ç¾îÁø´Ù.

Constructor: ostrstream::ostrstream ()
Create a new stream for output to a dynamically managed string, which will grow as needed.
µ¿ÀûÀ¸·Î °ü¸®µÇ´Â ÇÊ¿ä¿¡ ÀÇÇØ Ä¿Áö´Â ¹®ÀÚ¿­ Ãâ·ÂÀ» À§ÇÑ »õ·Î¿î ½ºÆ®¸²À» ¸¸µç´Ù.

Constructor: ostrstream::ostrstream (char* str, int size [,int mode])
A new stream for output to a statically defined string of length size, starting at str. You may optionally specify one of the modes described for ifstream::ifstream; if you do not specify one, the new stream is simply open for output, with mode ios::out.
str¿¡¼­ ½ÃÀÛÇÏ´Â ¹®ÀÚ¿­ ±æÀÌ°¡ sizeÀÎ Á¤ÀûÀ¸·Î Á¤ÀÇ µÈ »õ·Î¿î ½ºÆ®¸²ÀÌ´Ù. ifstream::ifstream·Î ¼³¸íµÈ ¸ðµå Áß Çϳª¸¦ ¼±ÅÃÇØ ¸í½ÃÇÒ ¼ö ÀÖ´Ù; ¸¸¾à ¸í½ÃÇÏÁö ¾ÊÀ¸¸é »õ·Î¿î ½ºÆ®¸²Àº iso::out ¸ðµå·Î °£´ÜÇÏ°Ô Ãâ·ÂÀ» À§ÇØ ¿­¸°´Ù.

Method: int ostrstream::pcount ()
Report the current length of the string associated with this ostrstream.
  ÀÌ ostrstream°ú ¿¬°áµÈ ¹®ÀÚ¿­ÀÇ ÇöÀç ±æÀ̸¦ º¸°í ÇÑ´Ù.

Method: char* ostrstream::str ()
A pointer to the string managed by this 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µÇ±â¸± ¹Ù¶õ´Ù¸é ½º½º·Î ±×°ÍÀ» Çؾ߸¸ ÇÑ´Ù(¾Æ¸¶µµ ½ºÆ®¸²ÀÇ ¾²±â ³¡¿¡).

Method: void ostrstream::freeze ([int n])
If n is nonzero (the default), declare that the string associated with this 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.

¸¸¾à nÀÌ 0ÀÌ ¾Æ´Ï¸é(±âº»°ªÀ¸·Î), ÀÌ 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´Â ºÒÇÊ¿äÇÏ´Ù. ±×¸®°í È¿°úµµ ¾ø´Ù.

Method: int ostrstream::frozen ()
Test whether freeze(1) is in effect for this string.
ÀÌ ¹®ÀÚ¿­À» À§ÇÑ È¿°ú¿¡ freeze(1)¸¦ Å×½ºÆ®ÇÑ´Ù.

Method: strstreambuf* strstreambase::rdbuf ()
A pointer to the underlying strstreambuf.
±âº»ÀÌ µÇ´Â strstreambuf Æ÷ÀÎÅÍ


óÀ½, ÀÌÀü, ´ÙÀ½, ¸¶Áö¸·, Â÷·Ê.