SoFunction
Updated on 2025-03-07

Detailed explanation of the example of #include header file in C++

fstream is a collection of file operations in C++ STL, which contains all commonly used file operations. In C++, all file operations are performed in a stream, and fstream is the file stream file stream.

The two most commonly used operations are:

1. Insert (<<)

Output data to the stream. For example, if a file stream fout is opened, then calling fout<<"Write to file"<<endl; means writing the string "Write to file" to the file and wrapping the line.

2. Disconnector (>>)

Enter data from the stream. For example, if the file stream fin is opened, then when the integer variable x is defined, fin>>x is to read an integer data from the file and store it in x.

Code Example

1. Write to the file

void baocun() {             //Write a file int i;
  ofstream booklist2("",ios::out);
  booklist2&lt;&lt;"bibliography     author   Current quantity   Total inventory";
  for(i=1; i&lt;=shuliang; i++) {
     booklist2&lt;&lt;endl;
     booklist2&lt;&lt;book[i].name&lt;&lt;"   "&lt;&lt;book[i].author&lt;&lt;"   "&lt;&lt;book[i].num&lt;&lt;"   "&lt;&lt;book[i].total&lt;&lt;endl;
  }
  ();
} 

2. Read the file

void addbook(string str){         //Add a book int i;
   for(i=1; i&lt;=shuliang; i++) {
     if(book[i].name==str) {
     int a;
     cout&lt;&lt;"Please enter the author's name:"&lt;&lt;endl;
     cin&gt;&gt;book[i].author;
     cout&lt;&lt;"Add number:"&lt;&lt;endl;
  cin&gt;&gt;a; 
       book[i].num+=a;
       book[i].total+=a;
       break;
     }
   }
   if(i==shuliang+1) {
     book[i+1]=book[i];
     book[i].name=str;
     int a;
     cout&lt;&lt;"Please enter the author's name:"&lt;&lt;endl;
     cin&gt;&gt;book[i].author;
     cout&lt;&lt;"Add number:"&lt;&lt;endl;
  cin&gt;&gt;a; 
     book[i].num+=a;
     book[i].total+=a;
     shuliang++;
   }
   cout&lt;&lt;"Added successfully"&lt;&lt;endl;
   cout&lt;&lt;"bibliography""&lt;&lt;book[i].name&lt;&lt;"》's total inventory is&lt;&lt;book[i].total&lt;&lt;", currently there"&lt;&lt;book[i].num&lt;&lt;"Ben.\n"&lt;&lt;endl;
   cout&lt;&lt;"Enter key returns to the main menu."&lt;&lt;endl;
   system("pause");
}

Replenish:

The difference between the #include header file in C++

<>: First look for header files in the system directory, then look for them in the current directory. It is generally used for standard header files and so on.

"  ": First look for the header file in the current directory, and then look for it in the system directory. It is generally used to include custom header files, so that the system prioritizes the use of the defined in the current directory.

Summarize

The above is a detailed explanation of the #include header file in C++ introduced to you by the editor. I hope it will be helpful to you!