[C++] iostream, fstream, stringstream knowledge

When I used to learn C++, I always swallowed up dates to understand cin, cout and other things. Recently, I was reviewing C++ and IO chapters. I felt a little touched. So I plan to record them.


As the saying goes, a picture is better than a thousand words, which is not unreasonable. Let's look at the inheritance structure of basic IO classes.



When we write simple C++ code, we like to write # include < iostream >, which is actually the header file of IO stream, but using namespace std; it means using standard space, so we can use cin, cout, endl and so on.

Seen from the figure above, IO can actually be classified into three categories: IO, IO, IO, IO, IO, IO, IO, IO, IO, IO, IO, IO, IO, IO, IO, IO, IO

1. iostream class: Responsible for dealing with console input and output, which we are familiar with. Note: Actually, it can be differentiated into istream and ostream.

2. fstream class: Responsible for dealing with file input and output, which we have contacted. Note: Actually, it can be distinguished as ifstream and ofstream.

Stringstream class: Responsible for dealing with input and output on strings, which we haven't used yet. Note: Actually, it can be distinguished as: istringstream and ostringstream.


Next, let's study/review one by one.

iostream of IO

Objects of the iostream class, such as CIN and cout, are directly associated with console input and output. Let's look at the simplest program:

  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. int main()  
  5. {  
  6.     int i = -1;  
  7.       
  8.     cin >> i; //cin receives input from the console and saves it in i  
  9.   
  10.     cout << i << endl; //count outputs the value of i to the console  
  11.   
  12.     return 0;  
  13. }  

It's easy to understand.


2. fstream of IO class values

The object of fstream, which is related to the file, is familiar to us. Look at the code directly.

  1. #include <iostream>  
  2. #include <string>  
  3. #include <fstream>  
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     ifstream in("test.txt"); //Establish a frontal association between in and file test.txt  
  9.     if(!in)  
  10.     {  
  11.         cout << "error" << endl;  
  12.         return 1;  
  13.     }  
  14.   
  15.     string line;  
  16.     while(getline(in, line))  
  17.     {  
  18.         cout << line << endl;     
  19.     }  
  20.   
  21.     return 0;  
  22. }  

3. stringstream of IO

stringstream objects are associated with string objects in memory, writing to string objects, or reading from string objects.

Let's first look at this question and assume that the content of test.txt is:

lucy 123 

lili 234 456

tom 222 456 535

jim 2345 675 34 654

The first word in each line is the name, and the number behind it is their bank card password. Of course, jim has four bank cards at most. Now, what should we do to achieve the following output?

lucy 123x 

lili 234x   456x 

tom 222x   456x   535x 

jim 2345x   675x   34x   654x 

Look directly at the procedure:

  1. #include <iostream>  
  2. #include <string>  
  3. #include <fstream>  
  4. #include <sstream>  
  5. using namespace std;  
  6.   
  7. int main()  
  8. {  
  9.     ifstream in("test.txt"); //Establish a frontal association between in and file test.txt  
  10.     if(!in)  
  11.     {  
  12.         cout << "error" << endl;  
  13.         return 1;  
  14.     }  
  15.   
  16.     string line;   
  17.     string password;  
  18.     while(getline(in, line))  
  19.     {  
  20.         istringstream ss(line); //Establishing the relationship between ss and line  
  21.         int i = 0;  
  22.         while(ss >> password) //ss reads things from line and saves them in password  
  23.         {     
  24.             cout << password + (1 == ++i ? "" : "x") << " ";  
  25.         }  
  26.           
  27.         cout << endl;  
  28.     }  
  29.   
  30.     return 0;  
  31. }  

The result is ok.

       

Next, let's look at how to use istringstream to convert strings to values:

  1. #include <iostream>  
  2. #include <string>  
  3. #include <sstream>  
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     int a = -1;  
  9.     string s = "101";  
  10.     istringstream is(s); //Establishing linkages  
  11.     cout << is.str() << endl; //101. It seems that is and s are indeed related.  
  12.   
  13.     is >> a;  
  14.   
  15.     cout << a << endl; // 101  
  16.   
  17.     return 0;  
  18. }  


Of course, we can also format numbers into strings, as follows (the following program has problems):

  1. #include <iostream>  
  2. #include <string>  
  3. #include <sstream>  
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     string str;  
  9.     ostringstream os(str); //Establish a relationship, but it's not related!!!  
  10.     os << "hello";  
  11.   
  12.     cout << str << endl;  //The str is empty. Why not "abc"? I don't understand.  
  13.   
  14.     return 0;  
  15. }  
It seems that the above connection is unsuccessful and should be replaced by:

  1. #include <iostream>  
  2. #include <string>  
  3. #include <sstream>  
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     int a = -1;  
  9.     ostringstream os;  
  10.     os << "hello" << a;  
  11.   
  12.     cout << os.str() << endl; // hello-1  
  13.   
  14.     return 0;  
  15. }  

Posted by voitek on Wed, 10 Apr 2019 21:09:32 -0700