#include <iostream.h> typedef long streampos; typedef long streamoff; class unsafe_ios { public: // exported types
// stream operation mode enum open_mode { in = 0x01, // open for reading out = 0x02, // open for writing ate = 0x04, // seek to eof upon original open app = 0x08, // append mode: all additions at eof trunc = 0x10, // truncate file if already exists nocreate = 0x20, // open fails if file doesn't exist noreplace= 0x40 // open fails if file already exists };
// stream seek direction enum seek_dir { beg=0, cur=1, end=2 };
// formatting flags enum { skipws = 0x0001, // skip whitespace on input left = 0x0002, // left-adjust output right = 0x0004, // right-adjust output internal = 0x0008, // padding after sign or base indicator dec = 0x0010, // decimal conversion oct = 0x0020, // octal conversion hex = 0x0040, // hexidecimal conversion showbase = 0x0080, // use base indicator on output showpoint = 0x0100, // force decimal point (floating output) uppercase = 0x0200, // upper-case hex output showpos = 0x0400, // add '+' to positive integers scientific= 0x0800, // use 1.2345E2 floating notation fixed = 0x1000, // use 123.45 floating notation unitbuf = 0x2000, // flush all streams after insertion stdio = 0x4000 // flush stdout, stderr after insertion };
// see ios(3CC4) for remainder ... }; class unsafe_istream : virtual public unsafe_ios { public: // exported functions // unformatted input functions unsafe_istream& read(char* ptr, int count); unsafe_istream& read(unsigned char* ptr, int count); unsafe_istream& get(char* ptr, int count, char delim='\n'); unsafe_istream& get(streambuf& sbuf, char delim='\n'); unsafe_istream& get(char& ch); unsafe_istream& get(unsigned char& ch); unsafe_istream& get_line (char *ptr, int count, char delim='\n'); unsafe_istream& get_line (unsigned char *ptr, int count, char delim='\n'); int get(); int peek(); unsafe_istream& ignore(int count=1, int delim=EOF); unsafe_istream& putback(char ch);
// wide character unsafe_istream& get(wchar_t* wptr, int count, wchar_t wdelim=L'\n'); unsafe_istream& get(wchar_t& wc); unsafe_istream& getline(wchar_t* wptr, int count, wchar_t wdelim=L'\n'); unsafe_istream& wignore(count=1, wdelim=WEOF); // other functions int ipfx(int noform=0); int wipfx(int noform=0); // wide character ipfx unsafe_istream& seekg(streampos pos); unsafe_istream& seekg(streamoff offset, unsafe_ios::seek_dir dir); streampos tellg(); int sync(); int gcount(); public: // exported operator functions unsafe_istream& operator>> (char* buf); unsafe_istream& operator>> (unsigned char* buf); unsafe_istream& operator>> (char&); unsafe_istream& operator>> (unsigned char&); unsafe_istream& operator>> (short&); unsafe_istream& operator>> (int&); unsafe_istream& operator>> (long&); unsafe_istream& operator>> (unsigned short&); unsafe_istream& operator>> (unsigned int&); unsafe_istream& operator>> (unsigned long&); unsafe_istream& operator>> (float&); unsafe_istream& operator>> (double&); unsafe_istream& operator>> (streambuf* sbufp); unsafe_istream& operator>> (unsafe_istream& (*manip)(unsafe_istream&)); unsafe_istream& operator>> (unsafe_ios& (*manip)(unsafe_ios&) ); public: // wide character unsafe_istream& operator>>(wchar_t&); unsafe_istream& operator>>(wchar_t*); public: // exported constructors unsafe_istream(streambuf* sbuf); }; class istream : virtual public ios, public unsafe_istream { public: //exported functions
// unformatted input functions istream& read(char* ptr, int count); istream& read(unsigned char* ptr, int count); istream& get(char* ptr, int count, char delim='\n'); istream& get(unsigned char* ptr,int count, char delim='\n'); istream& get(unsigned char& ch); istream& get(char& ch); istream& get(streambuf& sb, char delim ='\n'); istream& getline(char* ptr, int count, char delim='\n'); istream& getline(unsigned char* ptr, int count, char delim='\n'); int get(); int peek(); istream& ignore(int len=1,int delim=EOF); istream& putback(char ch);
// wide character istream& get(wchar_t* wptr, int count, wchar_t wdelim=L'\n'); istream& get(wchar_t& wc); istream& getline(wchar_t* wptr, int count, wchar_t wdelim=L'\n'); istream& wignore(int count=1, wchar_t wdelim=WEOF); wint_t peekw();
// other functions int ipfx(int noform=0); istream& seekg(streampos pos); istream& seekg(streamoff offset, seek_dir dir); streampos tellg(); int sync(); int gcount(); public: // exported operator functions istream& operator>>(char*); istream& operator>>(unsigned char*); istream& operator>>(char&); istream& operator>>(unsigned char&); istream& operator>>(short&); istream& operator>>(int&); istream& operator>>(long&); istream& operator>>(unsigned short&); istream& operator>>(unsigned int&); istream& operator>>(unsigned long&); istream& operator>>(float&); istream& operator>>(double&); istream& operator>>(streambuf*); istream& operator>>(istream& (*)(istream&)); istream& operator>>(ios& (*)(ios&)); public: // wide character istream& operator>>(wchar_t&); istream& operator>>(wchar_t*); public: // exported constructors istream(streambuf*);
};
class istream_withassign : public istream { public: istream_withassign(); istream_withassign& operator= (istream&); istream_withassign& operator= (streambuf*); }; extern istream_withassign cin; ios& dec(ios&); ios& hex(ios&); ios& oct(ios&); istream& ws(istream&);
unsafe_ios& dec(unsafe_ios&) ; unsafe_ios& hex(unsafe_ios&) ; unsafe_ios& oct(unsafe_ios&) ; unsafe_istream& ws(unsafe_istream&) ;
类 istream 支持从关联的 streambuf 执行有格式的和无格式的数据提取(输入)。
istream 对象及其成员函数使用互斥锁针对多个线程的访问提供保护。unsafe_istream 对象及其成员函数未实施互斥锁定。除此区别之外,istream 和 unsafe_istream 及其相应成员函数的功能完全相同。有关多线程安全性、互斥锁以及它们与 iostreams 对象有何关联的更多信息,请参见《MT-Safe libC Programmers Guide》。
.
将 sbufp 指向的 streambuf 与流相关联并初始化 ios 状态。
不执行初始化。
将 sbufp 指向的 streambuf 与 isw 相关联并完全初始化 isw。
与 istr 关联的 streambuf 将变为与 isw 关联并且构造函数将完全初始化 isw。
生成宽字符结果的输入函数如果遇到非法的字节序列会设置 failbit。如果输入函数无法使用当前的区域设置将字节序列解释为字符的多字节编码,则会将字节序列视为非法。
不要直接执行可以通过单字节输入操作提前窥视的多字节输入操作。允许在查找操作 (seekg) 之后执行单字节输入操作。streambufs 只需要支持提前一个字节窥视,因此,进行窥视的多字节输入操作可以在 istream 自身中缓冲“窥视的”字节。seekg 和 tellg 会考虑任何此类内部缓冲。
执行所有提取操作通用的设置。有格式的提取器调用 ipfx(0);无格式的提取器调用 ipfx(1)。如果 istr 的错误状态是非零值,则 ipfx 立即返回零。如果流绑定到 istr(参见 ios3cc4() 中的 tie)并且 noform 为零,则会刷新绑定的流。如果为流设置了 skipws 标志并且 noform 为零,则会跳过前导空格字符(由 iswhite 定义,请参见 ctype(3c))。如果遇到任何错误情况,函数 ipfx 将返回零,否则将返回非零值。
宽字符提取器使用 wipfx 取代 ipfx。这类似于 ipfx(),但 wipfx() 会跳过 iswspace 定义的任何多字节空格,而 ipfx 会跳过单字节空格字符。而且,如果遇到非法字节序列,wipfx() 会设置 ios::failbit。此函数不在建议的 ANSI iostreams 接口中,因此请避免直接从应用程序代码调用它。
这些函数调用 ipfx(0)。如果它返回零,则不会发生进一步的操作。否则,如果设置了 ios::skipws,则将删除前导空格。如果 istream 中只剩下空格,则不会保留任何字符,将设置 ios::failbit。宽字符有格式的输入函数调用 wipfx(0),并且如果它返回零,则不执行进一步的操作即返回。
从 istr 中提取字符并将其插入到 sbufp 指向的 streambuf 中。此函数始终返回对 istr 的引用。您可以使用此函数有效地复制流,但是应当确保流未绑定。例如:
#include <iostream.h> main() { // copy cin to cout cin.tie(0); cout.tie(0); cin >> cout.rdbuf(); // see ios(3CC4) for rdbuf return 0; }
从 istr 中提取字符并根据 x 的类型对其进行转换。如果 ipfx 返回零,则不会提取字符并且 x 不会改变。遇到的任何错误都将记录在 istr 的错误状态中。通常,ios::failbit 表示接下来的可用字符不适合该类型;例如,前导字母对于数字类型,或者前导空格对于任何类型。不会提取犯规的字符。通常,ios::badbit 表示没有字符可以提取;例如,尝试从文件末尾之后提取。这些函数始终返回对 istr 的引用。用户编写的函数应当采用格式
istream& operator>> (istream&, SomeType)
并且应当遵循这些原则。
x 的类型和 istream 的格式状态(请参见 ios (3cc4) )决定了提取和转换的细节。除了 width 变量在每次有格式的提取之后会重置为零之外,这些函数不更改 istream 的状态。预定义的有格式提取器如下所述:
提取一个字符并将其存储在 x 中。
如果 wipfx(0) 成功并且未遇到文件末尾,则提取一个多字节字符并将其作为宽字符存储在 x 中。如果遇到非法字节序列,则会设置 ios::failbit。
提取字符并根据 istream 的格式标志中的转换基将其转换为整数值,然后将值存储在 x 中。第一个字符可以是加号 ('+') 或减号 ('-')。如果 ios::basfield 标志为 dec、oct 或 hex,则字符将分别作为十进制、八进制或十六进制字符进行处理。如果未设置这些标志中的任何一个,则按照与 C++ 中的整数常量相同的方式解释数字。也就是说,如果前两个字符是 "0x" 或 "0X",则基为十六进制;否则,如果第一个字符是 '0',则基为八进制;否则,基为十进制。提取(以及随后的转换)将在第一个非数字位处停止,并且不会提取该位。对于八进制,有效数位为 '0'-'7';对于十进制,有效数位为 '0'-'9';对于十六进制,有效数位为 '0'-'9'、'a'-'f'、'A'-'F'。如果未遇到有效数位,则会设置错误标志 ios::failbit。"0x" 后跟非数字位被视为错误。当遇到错误时,x 的值不会更改。
提取字符并根据针对浮点数常量的 C++ 规则对它们进行转换,然后将值存储在 x 中。如果提取的字符不能作为格式正确的浮点数的开头,则会设置错误标志 ios::failbit;在这种情况下可能会提取某些字符,具体取决于错误是在哪个位置点检测到的。当遇到错误时,x 的值不会更改。
提取字符并将其存储在 x 指向的数组中,直到遇到空格字符。不会提取空格(但 ipfx 可能已丢弃了前导空格)。如果 width 格式设置变量(请参见 ios (3cc4) )为非零值,则至少会提取 width-1 个字符。始终会在 x 中存储一个终止空值 (0),即使什么都未提取也是如此。如果没有字符可供提取,则会设置错误标志 ios::failbit。
与 char* 的提取器的行为类似,但是它会将以多字节表示的字符解码为宽字符数组,直到遇到多字节空格字符。此外,如果遇到非法的字节序列,它还会设置 ios::failbit。
这些函数调用 ipfx(1)。如果它返回零,则不会发生进一步的操作。不会跳过前导空格,并且不会进行转换。宽字符输入函数调用 wipfx(1) 而不是 ipfx(1)。
从 istr 中提取下一个字符并将其返回。如果没有更多字符可用,则返回 EOF;从不会设置 ios::failbit。
从 istr 中提取下一个字符并将其存储在 ch 中。如果没有更多字符可用,则会存储 EOF;当尝试从 EOF 之后提取时会设置 ios::failbit。此函数始终返回对 istr 的引用。
从 istr 中提取字符并将其存储在开头位于 ptr 的 char 数组中。提取在提取 count-1 个字符后停止或者在遇到与 delim 匹配的字符时停止,具体取决于先发生哪种情况。如果遇到 delim 字符,不会提取或存储该字符。此函数始终会存储一个终止空值 (0),即使什么都未提取也是如此。只有未存储任何字符就遇到了 EOF 时才会设置 ios::failbit。此函数始终返回对 istr 的引用。
从 istr 中提取字符并将其存储在 streambuf sbuf 中。提取在遇到与 delim 匹配的字符(或 EOF)时停止,或者在向 sbuf 中进行存储失败时停止,具体取决于先发生哪种情况。如果遇到 delim 字符,不会提取或存储该字符。如果 delim 为 EOF,则提取只会在输入耗尽时或存储失败时停止。只有当向 sbuf 中进行存储失败时,此函数才会设置 ios::failbit;它始终返回对 istr 的引用。
从 istr 中提取下一个字符并以其宽字符表示形式将其存储在 wc 中。如果没有更多字符可用,则会存储 WEOF;当尝试在文件末尾之后提取时或者遇到非法字节序列时会设置 ios::failbit。返回对 istr 的引用。
与针对单字节字符的情况相同,但是 wptr 指向 wchar_t 的数组并且 wdelim 的类型为 wchar_t。如果遇到非法字节序列,则会设置 ios::failbit。返回对 istr 的引用。
此函数执行与 istr.get(ptr, count, delim) 相同的功能,不同的是,如果遇到 delim,会提取但不存储该字符。在提取 count-1 个字符之后,下一个字符将留在 istr 中,即使它是 delim。此函数始终返回对 istr 的引用。
与针对单字节字符的情况相同,但是 wptr 指向 wchar_t 的数组并且 wdelim 的类型为 wchar_t。如果遇到非法字节序列,则会设置 ios::failbit。返回对 istr 的引用。
从 istr 中提取字符并丢弃它们。当遇到与 delim 匹配的字符时、提取了 count 个字符后或者当遇到 EOF 时提取停止,具体取决于先发生哪种情况;会提取 delim 字符。如果 delim 是 EOF,则会丢弃所有输入。此函数始终返回对 istr 的引用。
与针对 ignore() 的情况相同,但如果 wdelim 不是 WEOF,则 count 指示要跳过的多字节字符数而不是指示要跳过的字节数。即使 wdelim 是 WEOF,如果遇到非法字节序列,它也会设置 ios::failbit 并停止。返回对 istr 的引用。
从 istr 中提取字符并将其存储在开头位于 ptr 的 char 数组中。提取在提取 count 个字符后停止或者在遇到 EOF 时停止,具体取决于先发生哪种情况。如果在提取 count 个字符之前遇到 EOF,则此函数会设置 ios::failbit;它始终返回对 istr 的引用。可以通过调用 istr.gcount() 查明提取的字符数。(请参见下文。)
这些函数处理指向与 istream 关联的 streambuf 的 get 指针。有关完整论述,请参见 sbufpub (3CC4) 。多字节输入操作可能会导致 streambuf 的 get 指针不同于 tellg() 报告的值,但是在 istream 自身上执行的查找仍将与 istream 上的所有操作正确协调。
设置 get 指针的位置;返回 istr。
设置 get 指针的位置;返回 istr。
返回 get 指针的当前位置。多字节输入操作可能会导致 streambuf 的 get 指针不同于 tellg() 报告的值,但是在 istream 而非 streambuf 上执行的查找仍将与 istream 上的所有操作正确协调。
返回由最后一个无格式输入函数从 istr 中提取的字符数。注意:有格式的输入函数可以调用无格式的函数,这样会更改计数。在无格式的宽字符输入函数之后,gcount() 返回提取的(多字节)字符数。
首先会调用 istr.ipfx(1)。如果 ipfx 返回零或者 istr 位于 EOF,则它将返回 EOF。否则,它将返回下一个字符但不提取它。
调用 wipfx(1),如果失败,将返回 (wint_t)WEOF。如果遇到非法字节序列,则会设置 ios::failbit 并返回 WEOF。如果成功,则会将 stream 中的下一个字符作为宽字符返回,但不会移过该字符。
如果 istr.fail() 返回非零值,则此函数不执行任何操作便返回。否则,它将尝试通过将字符 ch 推回原位来备份与 istr 关联的 streambuf。因为它不执行提取,所以不调用 ipfx。它可能会失败并设置 ios::failbit。通常,c 必须与 streambuf 的 get 指针之前的字符(通常是所提取的最后一个字符)匹配-输入可以来自只读内存中缓冲区。
以实现定义的方式在内部数据结构与外部字符来源之间强制实施对应关系。它调用虚拟函数 istr.rdbuf()->sync(),后者当然取决于缓冲区类的实际类型。发生错误时,它返回 EOF。
操纵符可以明确地用作插入的或提取的对象,但许多仅更改流的状态。有关更多信息,请参见 manip(3cc4) 和 ios(3cc4)。预定义了多个与 istream 一起使用的操纵符。
这等效于调用 manip(istr)。
它将 istr 的转换基设置为 10。
它将 istr 的转换基设置为 8。
它将 istr 的转换基设置为 16。
它从 istr 中提取并丢弃连续的空格字符。
ctype (3C) 、 ios (3CC4) 、 ios.intro (3CC4) 、 manip (3CC4) 、 sbufpub (3CC4) 、 stdiobuf (3CC4)
《C++ Library Reference》中的第 3 章 "The Classic iostream Library" 和第 4 章 "Using Classic iostreams in a Multithreaded Environment"。