C++ Standard Library: iostream and File Operations

C++ Standard Library: iostream and File Operations

The C++ Standard Library provides powerful input and output (I/O) capabilities, among which <span>iostream</span> is one of the most commonly used classes. Through <span>iostream</span>, we can perform basic console input and output, while file operations allow us to handle data more flexibly.

Overview of iostream

<span>iostream</span> is a set of classes used for handling input and output, including <span>istream</span>, <span>ostream</span>, and their combination <span>iostream</span>.

  • istream: Used for input streams, such as reading data from the keyboard.
  • ostream: Used for output streams, such as printing information to the screen.
  • iostream: A stream that supports both input and output.

1. Using cout to Output to Console

Here is a simple example demonstrating how to use <span>cout</span> to output text:

#include <iostream>
int main() {    std::cout << "Hello, world!" << std::endl;    return 0;}

In this code:

  • We include the <span><iostream></span> header file to use the standard I/O classes.
  • We use <span>std::cout</span> to print a string to the console, followed by a newline character.

2. Using cin to Read User Input from Console

Next, let’s look at how to receive user input from the console:

#include <iostream>
#include <string>
int main() {    std::string name;
    std::cout << "请输入你的名字: ";    std::cin >> name;
    std::cout << "你好, " << name << "!" << std::endl;
    return 0;}

In the above code:

  • We first define a string variable to store the user’s name.
  • We use <span>std::cin</span> to receive input from the keyboard and store it in the variable.

Overview of File Operations

The C++ Standard Library also provides methods for reading and writing files. This is mainly implemented through the <span>fstream</span>, <span>ifstream</span>, and <span>ofstream</span> classes.

Here is a brief description of each class:

  • ifstream: Input file stream, used for reading file contents.
  • ofstream: Output file stream, used for writing content to files.
  • fstream: File stream that can perform both read and write operations.

3. Creating and Writing to a File (ofstream)

Below is an example demonstrating how to create a new file and write content to it:

#include <fstream>
#include <string>
int main() {    std::ofstream outFile("example.txt");
    if (outFile.is_open()) {        outFile << "这是第一行。\n";        outFile << "这是第二行。" << std::endl;                outFile.close(); // Close the file after writing        std::cout << "成功写入到example.txt" << std::endl;    } else {        std::cerr << "无法打开或创建example.txt" << std::endl;    }
    return 0;}

In this code:

  • We use a new text file named <span>"example.txt"</span>. If the file does not exist, it will be created automatically.
  • After checking if it opened successfully, we write some text to the document and finally close it to save the changes.

4. Reading from a File (ifstream)

Now, this example demonstrates how to read content from the previously created data:

#include <fstream>
#include <string>
#include <iostream>
int main() {    std::ifstream inFile("example.txt");
    if (inFile.is_open()) {        std::string line;
        while (std::getline(inFile, line)) { // Read line by line            std::cout << line + "\n"; // Print each line         }
        inFile.close(); // Close the file after completion    } else {       std::cerr << "无法打开example.txt" << std::endl;    }
   return 0; }

In this code:

  • We attempt to open the previously created <span>"example.txt"</span> file for reading information. If successful, it reads and prints its content line by line to the console.

Conclusion

Today, we learned the basic I/O operations in the C++ Standard Library, including basic interactions with the console and simple reading/writing of text data on disk. With these fundamentals mastered, you can further explore more complex data structures and algorithms while leveraging C++’s rich and efficient data processing capabilities. We hope this tutorial helps you successfully start your programming journey.

Leave a Comment