一、iostream简介
iostream是C++标准库中的重要组成部分,用于在C++程序中进行输入输出操作。其中,i代表输入,o代表输出,而stream代表了数据流。它是面向对象的,内置了一些底层操作,使得我们可以更方便地进行文件和控制台的输入输出操作。
二、iostream的基础操作
iostream底层使用了缓存机制,所以我们需要使用flush来将缓存中的数据输出到终端中,同时为了保证终端窗口中数据的双向通信,需要在每次输出完毕后使用endl来结束一行语句并刷新缓存。
#include <iostream>
using namespace std;
int main()
{
int num = 125;
cout << "num的值为:" << num << endl;
cout <> in_num;
cout << "输入的整数是:" << in_num << endl;
return 0;
}
三、iostream的高级操作
iostream还支持对文件操作的输入输出。我们可以利用fstream头文件中的ifstream和ofstream来分别实现文件的读取和输出。在打开文件时,需要使用open()函数将文件名和打开方式一起传入。读取文件时,使用getline()函数可以逐行读取文件中的内容。
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream outfile;
outfile.open("test.txt", ios::out);
outfile << "hello world!" << endl;
outfile.close();
ifstream infile;
infile.open("test.txt", ios::in);
string s;
if(infile.is_open()){
while(getline(infile, s)){
cout << s << endl;
}
}
infile.close();
return 0;
}
四、iostream的格式化操作
iostream支持多种格式化操作,如setw()函数可以设置输出数据字段的宽度,setprecision()函数可以设置输出小数的精度等。
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double pi = 3.1415926535;
cout << "不设置精度:" << pi << endl;
cout << "设置精度为3:" << setprecision(3) << pi << endl;
cout << "字段宽度为10:" << setw(10) << pi << endl;
return 0;
}
五、iostream的异常处理
iostream能够捕获并处理输入输出的异常。例如,如果我们想要输入一个整数,但用户输入了一个非法字符,则会抛出异常并终止程序。我们可以使用try-catch语句来处理异常,并将程序的控制权交还给用户。
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
try{
int num;
cout <> num;
cout << "输入的整数是:" << num << endl;
} catch(ios_base::failure &ex){
cout << "输入的不是整数!" << endl;
}
return 0;
}
六、iostream的运算符重载
我们可以在自己的类中重载iostream的输入输出运算符,实现对象在控制台中的输入输出。例如,我们可以在一个名为Score的类中重载运算符<>,以实现该类在控制台中的输入输出。
#include <iostream>
using namespace std;
class Score{
public:
Score(){}
Score(int a, int b){
math = a;
english = b;
}
friend ostream& operator<<(ostream& os, Score& sc)
{
os << "Math: " << sc.math << " English: " <>(istream& is, Score& sc)
{
cout<> sc.math >> sc.english;
return is;
}
private:
int math;
int english;
};
int main()
{
Score x;
cin >> x;
cout << x << endl;
return 0;
}
七、iostream的综合运用
下面是一份iostream的综合运用代码示例,包括了文件写入、读取、格式化输出等内容。程序将读取txt文件中的成绩单信息,并将每个学生的平均分输出到txt文件中。我们同时还可以看到如何使用点操作符、异常处理和重载运算符等高级特性。
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
class Student{
public:
string name;
int math, english;
Student(){}
Student(string n, int m, int e){
name = n;
math = m;
english = e;
average = (m + e) / 2;
}
double getAverage(){
return average;
}
friend ostream& operator<<(ostream& os, Student& sc)
{
os << setw(10) << sc.name;
os << setw(8) << sc.math;
os << setw(8) << sc.english;
os << setw(10) << setprecision(3) << sc.average <>(istream& is, Student& sc)
{
is >> sc.name >> sc.math >> sc.english;
if (is.fail()) {
throw ios_base::failure("无效的输入!");
}
sc.average = (sc.math + sc.english) / 2.0;
return is;
}
private:
double average;
};
int main()
{
ifstream infile;
infile.open("grades.txt", ios::in);
ofstream outfile;
outfile.open("result.txt", ios::out | ios::trunc);
outfile << "name\t math\t english\t avg" <> s){
try{
outfile << s;
} catch (const ios_base::failure &ex){
cerr << ex.what() << endl;
}
}
outfile.close();
infile.close();
return 0;
}
原创文章,作者:LOUJK,如若转载,请注明出处:https://www.506064.com/n/360359.html
微信扫一扫
支付宝扫一扫