I. Input and output of the file
Header file FStream defines three types of supporting files Io:ifstream reading data from a given file, Ofstream writing data to a given file, and fstream reading to the specified data. These types are like the operations of CIN and cout, we can read and write files with the IO operator, and we can use Getline to fetch data from a ifstream.
1. Getline () function
The function prototypes for Getline are:
[CPP]View Plaincopy
- istream& Getline (istream& is, string& str, char delim);
- istream& Getline (istream&& is, string& str, char delim);
- istream& Getline (istream& is, string& str);
- istream& Getline (istream&& is, string& str);
Usually we use the Getline function to read an entire line, which takes an input stream and a string object, and the function reads the content from the given input stream until it encounters a newline character, and then deposits the read into a string object.
In addition, when the function is istream& getline (istream& is, string& str, char delim), the function encounters Delim also stops.
2. Using File Stream objects
When we want to read a file, we can define a file stream object and associate the object with the file, each of which defines a member function called open to complete a series of system-related operations.
The prototype of the Open function is:
[CPP]View Plaincopy
- VOID Open (const char* filename, ios_base::openmode mode = ios_base::out);
- VOID Open (const string& filename, Ios_base::openmode mode = ios_base::out);
There are several types of file modes (mode):
[CPP]View Plaincopy
- Ofstream outfile ("E:\\out.txt", Ofstream::app);
The code above opens the OUT.txt file, and if it does not exist, the system creates the TXT file and navigates to the end of the file.
Open files must be closed after use, FStream provides the member function close () to complete this operation.
Example: reading data from a hello.txt file and writing it to OUT.txt
[CPP]View Plaincopy
- #include "stdafx.h"
- #include <vector>
- #include <string>
- #include <fstream>
- #include <iostream>
- Using namespace std;
- int _tmain (int argc, _tchar* argv[])
- {
- Ifstream myfile ("E:\\hello.txt");
- Ofstream outfile ("E:\\out.txt", Ofstream::app);
- string temp;
- if (!myfile.is_open ())
- {
- cout << "File not successfully opened" << Endl;
- }
- While (Getline (myfile,temp))
- {
- outfile<<temp;
- }
- Myfile.close ();
- return 0;
- }
Two, a string stream
The string header file defines three types to support memory Io,istringstream writing data to a string, Ostringstream reading data from a string, StringStream can read data from a string or write data to a string, just as a string is an IO stream.
1, the use of Istringstream
[CPP]View Plaincopy
- #include "stdafx.h"
- #include <string>
- #include <sstream>//header files required for the use of Istringstream
- #include <iostream>
- Using namespace std;
- int _tmain (int argc, _tchar* argv[])
- {
- String str = "I am a Boy";
- Istringstream is (str);
- string S;
- While (is >> s)
- {
- cout << s << endl;
- }
- return 0;
- }
The output is:
I
Am
A
Boy
Example: Write a program that saves rows from a file in a vector<string>, and then uses Istringstream to read a data element from a vector, reading one word at a time.
[CPP]View Plaincopy
- #include "stdafx.h"
- #include <vector>
- #include <string>
- #include <fstream>
- #include <sstream>
- #include <iostream>
- Using namespace std;
- int _tmain (int argc, _tchar* argv[])
- {
- Vector<string> VEC;
- Ifstream myfile ("E:\\hello.txt");
- string temp;
- if (!myfile.is_open ())
- {
- cout << "File not successfully opened" << Endl;
- }
- While (Getline (myfile,temp))
- {
- Vec.push_back (temp);
- }
- For (Auto it = Vec.begin (); It! = Vec.end (); it++)
- {
- cout << *it << Endl;
- }
- cout << "-----------------use Istringstream------------------------" << Endl;
- For (Auto it = Vec.begin (); It! = Vec.end (); it++)
- {
- Istringstream record (*it);
- string S;
- While (record >> s)
- cout << s << endl;
- }
- return 0;
- }
Operation Result:
The following discussion is transferred from www.cndev-lab.com, program author: Guan Ning
[CPP]View Plaincopy
- #i nclude <iostream>
- #i nclude <sstream>
- Using namespace std;
- int main ()
- {
- Istringstream ISTR;
- Istr.str ("1 56.7",);
- //The above two processes can be simply written as Istringstream istr ("1 56.7");
- cout << istr.str () <<endl;
- int A;
- float B;
- istr>>a;
- cout<<a<<endl;
- istr>>b;
- cout<<b<<endl;
- System ("pause");
- }
In the above example, when constructing a string stream, the space becomes the internal boundary of the string parameter, and the input "assignment" operation of a A/b object in the example proves this, and the space of the string becomes the decomposition point of the integer data and the floating-point data. Using the method of demarcation we actually completed the split conversion process of string-to-integer object and floating-point object.
The use of the STR () member function allows the Istringstream object to return a string string, such as the output operation in this example (Cout<<istr.str ();).
2, the use of Ostringstream
Ostringstream is also constructed from a string object, and the Ostringstream class inserts a character into a string.
The Ostringstream constructor is as follows:
[CPP]View Plaincopy
- Ostringstream::ostringstream (string str);
The following discussion is transferred from www.cndev-lab.com, program author: Guan Ning
[CPP]View Plaincopy
- #i nclude <iostream>
- #i nclude <sstream>
- #i nclude <string>
- Using namespace std;
- int main ()
- {
- Ostringstream Ostr;
- //ostr.str ("abc");//If a string parameter is set at the time of construction, then the increment operation does not increase from the end, but modifies the original data, and the excess portion grows
- Ostr.put (' d ');
- Ostr.put (' e ');
- ostr<<"FG";
- String gstr = Ostr.str ();
- cout<<gstr;
- System ("pause");
- }
In the previous example code, we can continuously insert a single character or string into the ostr by using the put () or left-shift operator, and return the full string data after the growth through the STR () function, but it is worth noting that when the object is constructed with string data in it, Then the growth operation will not increase from the end, but modify the original data, beyond the growth of the part.
C + + Learning Note-c++ Read and write to TXT file