📄 ifstream.htm
字号:
<html>
<head>
<title> The basic_ifstream(ifstream) Class</title>
</head>
<body bgcolor="#FFFFFF">
<img src="ifstrban.gif">
<pre>
<font size=5>Class Name</font> basic_ifstream
<font size=5>Header File</font> <fstream>
<font size=5>Classification</font> Input/Output (Format Class)
</pre>
<a href="ifstream.htm#diagram">Class Relationship Diagram</a><br>
<br>
<a href="ifstream.htm#desc">Class Description</a><br>
<h1>Member Classes</h1>
<a href="filebuf.htm">basic_filebuf</a>
<h1>Methods</h1>
<pre>
<a href="ifstream.htm#ifstream1">basic_ifstream()</a><br>
<a href="ifstream.htm#ifstream2">explicit basic_ifstream(const char* s,ios_base::openmode mode = ios_base::in)</a><br>
<a href="ifstream.htm#rdbuf">basic_filebuf<charT,traits>* rdbuf() const</a><br>
<a href="filebuf.htm#is_open">bool is_open()</a><br>
<a href="filebuf.htm#open">void open(const char* Str, openmode OMode = in)</a><br>
<a href="filebuf.htm#close">void close()</a><br>
</pre>
<a href="ifstream.htm#example"><h2>Example</h2></a><br>
<hr>
<a name="desc"><h3>Class Description</h3></a>
<p>
The ifstream class is derived from the basic_istream class. The ifstream class models the input
file stream. Whereas the basic_istream class focuses on a input stream normally attached to the
console, the ifstream focuses on a stream normally attached to a file. The ifstream is a
specialized basic_istream class. Through the filebuf component the ifstream class has access to
the file. It is important to note that the ANSI/ISO standard for the iostreams does not support
the concept of a file descriptor. Older implementations of the iostreams often included file
descriptor access through the filebuf class and some type of fd() method. This type of access
is no longer supported. The standard typedef for basic_ifstream is <i>ifstream</i>.
</p>
<hr>
<pre>
<a name="ifstream1">Method basic_ifstream()</a>
Access Public
Classification Constructor
Syntax basic_ifstream()
Parameters None
Return None
</pre>
<h3>Description</h3>
<p>
This basic_ifstream() method constructs a basic_ifstream object. It constructs a
stream that is not connected to any file or device. The open() method will have to
be called to connect the stream with a file or device.
</p>
<hr>
<pre>
<a name="ifstream2">Method basic_ifstream()</a>
Access Public
Classification Constructor
Syntax explicit basic_ifstream(const char* Str,ios_base::openmode OMode = ios_base::in)
Parameters Str points to the name of the file or device that will be opened and
connected to the ifstream object. OMode is the mode the stream will
be opened in.
Return None
</pre>
<h3>Description</h3>
<p>
This basic_ifstream() method constructs a basic_ifstream object. This constructor constructs a
stream sthat will be connected to the device named by Str. The device or file will be opened
in the mode specified by OMode. This constructor opens the file specified by Str.
if the file cannot be opened the buffer state will contain the error condition.
</p>
<hr>
<pre>
<a name="rdbuf">Method rdbuf()</a>
Access Public
Classification Accessor
Syntax basic_filebuf<charT,traits>* rdbuf() const
Parameters None
Return Returns a pointer to the associated streambuf object.
</pre>
<h3>Description</h3>
<p>
The rdbuf() method returns a pointer to the associated streambuf object.
</p>
<hr>
<p>
<a name="diagram"><h2>The Class Relationship Diagram of basic_ifstream</h2></a>
<img src="ifstream.gif">
</p>
<hr>
<a name="example"></a>
<img src="lego.gif">
<pre>
1 // This program reads in two sorted files and performs a merge sort
2 // to the predefined stream cout.
3 // It demonstrates that the programmer can declare an array
4 // of stream objects.
5
6 #include <fstream>
7 #include <stdlib.h>
8
9 ifstream Stream[2];
10
11
12 void main(void)
13
14 {
15
16 double Object[2];
17 Stream[0].open("list1.txt");
18 if(!Stream[0].good()){
19 cerr << "could Not open file 1";
20 exit(0);
21 }
22 Stream[1].open("list2.txt");
23 if(!Stream[1].good()){
24 cerr << "could not open file 2 ";
25 exit(0);
26 }
27 Stream[0] >> Object[0];
28 Stream[1] >> Object[1];
29 while(!Stream[0].eof() && !Stream[0].fail() &&
30 !Stream[1].eof() && !Stream[1].fail())
31 {
32
33 if(Object[0] < Object[1]){
34 cout << Object[0] << endl;
35 Stream[0] >> Object[0];
36 }
37 else
38 if(Object[1] < Object[0]){
39 cout << Object[1] << endl;
40 Stream[1] >> Object[1];
41 }
42 else{
43 cout << Object[0] << endl << Object[1] << endl;
44 Stream[0] >> Object[0];
45 Stream[1] >> Object[1];
46 }
47
48 }
49 while(!Stream[0].eof() && !Stream[0].fail())
50 {
51 Stream[0] >> Object[0];
52 cout << Object[0] << endl;
53
54 }
55 while(!Stream[1].eof() && !Stream[1].fail())
56 {
57
58 Stream[1] >> Object[1];
59 cout << Object[1] << endl;
60
61 }
62 Stream[0].close();
63 Stream[1].close();
64
65 }
</pre>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -