从0开始学习C++ 第二课:C++中的输入和输出(I/O)

第二课:C++中的输入和输出(I/O)

学习目标:

  • 理解C++中的输入输出流的概念及其工作原理。
  • 学习如何使用iostream库中的cincout对象进行基本的输入输出操作。
  • 掌握格式化输出的方法,包括如何设置宽度、填充和精度。
  • 学习如何从文件读取数据以及向文件写入数据。

学习内容:

  1. 输入输出流简介:

    • 输入输出流提供了数据的输入和输出功能。在C++中,这通常通过iostream库实现,它包含了用于输入的cin对象,用于输出的cout对象,以及用于输出错误信息的cerrclog对象。
  2. 基本的输入输出:

    • 使用cin进行输入,cout进行输出。

    代码示例:

    #include <iostream>
    using namespace std;
    
    int main() {
        int number;
        cout << "Enter an integer: ";
        cin >> number;
        cout << "You entered: " << number << endl;
        return 0;
    }
    

    预计输出效果:

    Enter an integer: 5
    You entered: 5
    
  3. 格式化输出:

    • 使用iomanip库中的函数如setw(设置宽度)、setfill(设置填充字符)和setprecision(设置小数点精度)进行输出格式控制。

    代码示例:

    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main() {
        double pi = 3.14159265358979323846;
        cout << "Pi rounded to 3 decimal places: " << setprecision(3) << fixed << pi << endl;
        cout << "Pi with setw(10): " << setw(10) << pi << " end" << endl;
        cout << "Pi with setw(10) and setfill('*'): " << setfill('*') << setw(10) << pi << " end" << endl;
        return 0;
    }
    

    预计输出效果:

    Pi rounded to 3 decimal places: 3.142
    Pi with setw(10):    3.14159 end
    Pi with setw(10) and setfill('*'): ***3.14159 end
    
  4. 文件输入输出:

    • 使用fstream库的ifstream类进行文件读取和ofstream类进行文件写入。

    代码示例:

    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main() {
        // 写入文件
        ofstream outfile("example.txt");
        outfile << "This is an example text." << endl;
        outfile.close();
    
        // 读取文件
        ifstream infile("example.txt");
        string line;
        if (infile.is_open()) {
            while (getline(infile, line)) {
                cout << line << '\n';
            }
            infile.close();
        } else {
            cout << "Unable to open file";
        }
        return 0;
    }
    

    预计输出效果:

    This is an example text.
    

练习题:

编写一个程序,要求用户输入他们的全名和年龄。然后程序将这些信息格式化输出到一个文本文件中,例如:“Name: [FullName], Age: [Age]”。注意处理文件读写时的异常情况。

答案:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
    string full_name;
    int age;

    cout << "Enter your full name: ";
    getline(cin, full_name);
    cout << "Enter your age: ";
    cin >> age;

    ofstream outfile("user_info.txt");
    if (outfile.is_open()) {
        outfile << "Name: " << full_name << ", Age: " << age << endl;
        outfile.close();
        cout << "Information successfully saved to file." << endl;
    } else {
        cerr << "Unable to open file for writing." << endl;
    }

    return 0;
}

预计输出效果(示例):

Enter your full name: John Doe
Enter your age: 30
Information successfully saved to file.

在文件user_info.txt中,内容将是:

Name: John Doe, Age: 30

通过这一课,学生应该能够掌握基本的输入输出操作,了解如何格式化输出数据以及如何将数据读写到文件中。这些技能对于日常编程非常重要,因为它们是与用户或其他系统交换信息的基础。

目录
第三课:变量和基本数据类型

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值