(1) cin,表示标准输入的istream类对象。cin从终端读入数据
    (2) cout,表示标准输出的ostream类对象。cout向终端写数据
    (3) cerr,表示标准错误输出(非缓冲方式),导出程序错误消息
    (4) clog,表示标准错误输出(缓冲方式),导出程序错误消息

    • C++语言中常用的4个标准输入/输出流为cin、cout、cerr和clog。用于基本数据类型的操作。
    • 重载的操作符<<和>>为用户自定义数据类型的输入输出提供了方便。
    • 在文件处理中,分文本文件流和二进制文件流两种,C++提供了对两种文件处理的istream类和ostream类,并提供顺序访问和随机访问两种机制。

      1. #include <iostream>
      2. #include<fstream>
      3. /*
      4. #include<ifstream>
      5. #include<ofstream>
      6. */
      7. using namespace std;
      8. void read(Student stu[])//首先从文件读入原有学生信息
      9. {
      10. cout<<" 学号 "<<" 姓名 " <<" 性别 "<<"班级 "<<" 院系 "<<""<<" 宿舍号 "<<""<<" 手机号 "<<endl;
      11. ifstream in("cduxie.txt");
      12. if(in)
      13. {
      14. for(int i=1;i<=Init;i++)
      15. {
      16. in>>stu[i].m_no>>stu[i].m_name>>stu[i].m_sex>>stu[i].m_class_name>>stu[i].m_depart>>stu[i].m_num>>stu[i].phone_num;
      17. cout<<stu[i].m_no<<setw(10)<<stu[i].m_name<<setw(5)<<stu[i].m_sex<<" "<<stu[i].m_class_name<<" "<<stu[i].m_depart<<" "<<stu[i].m_num<<" "<<stu[i].phone_num<<endl;
      18. Student::m_Enum++;
      19. }
      20. }
      21. in.close(); 文件关闭
      22. }
      23. void write(Student stu[])//输出到文件
      24. {
      25. ofstream out("cduxie1.txt");
      26. if(out)
      27. {
      28. for(int i=1;i<=Student::m_Enum;i++)
      29. out<<stu[i].m_no<<setw(10)<<stu[i].m_name<<setw(5)<<stu[i].m_sex<<" "<<stu[i].m_class_name<<" "<<stu[i].m_depart<<" "<<stu[i].m_num<<" "<<stu[i].phone_num<<endl;
      30. }
      31. out<<"共有:"<<Student::m_Enum<<"名学生"<<endl;
      32. out.close();
      33. }
      1. #include<iostream>
      2. #include<fstream>
      3. using anmespace std;
      4. int main(){
      5. fstream fio("cduxie1.txt",ios::in|ios::out);//可读可写
      6. if(fio.fill())//判断打开是否成功
      7. {
      8. cerr<<"打开失败;"<<endl;
      9. return 0;
      10. }
      11. while((c=fio.get()!=EOF)//是否到达文件末尾
      12. if(isupper(c))
      13. {
      14. }
      15. fio.close();
      16. return 0;
      17. }

      ```cpp 方法1.ios类成员函数控制格式 (1) 设置状态标志状态标志字用于控制格式,可以查看/更改,见表9-1。 stream_obj.setf(ios::enum_type); 说明:其中,stream_obj为被影响的流对象(cin或cout),enum_type为状态标识字,如表9-1。例如: cout.setf(ios::dec); 表示以十进制输出。 cout.setf(ios::dec|ios::uppercase); 十进制且大写

    (2) 清除状态标志 stream_obj.unsetf(ios::enum_type);

    (3) 取状态标志 状态标志字用long int型(16位)存储,每一位对应一种状态 long ios::flags(); //返回当前状态标志字 long ios::flags(long flag); //将状态标志字设置为flag,并放回原状态标志字

    (4)设置域宽:
    int ios::width()//显示带宽 int ios::width(int w)//设置带宽 ,并返回原带宽

    (5)设置填充字符:
    char ios::fill() //显示填充字符 char ios::fill(char c) //设置填充字符,并返回原填充字符

    (6)设置显示精度:
    int ios::precision() //显示精度 int ios::precision(int num)//设置精度,并返回原精度

    1. ```cpp
    2. ostream &自定义输出操纵符算子函数名(ostream& stream, class_name& obj)
    3. {
    4. …… //自定义代码
    5. return stream;
    6. }
    7. istream &自定义输入操纵符算子函数名(istream& stream, class_name& obj)
    8. {
    9. …… //自定义代码
    10. return stream;
    11. }
    12. [例9-10] 自定义输出操作函数。
    13. #include <iostream>
    14. #include <iomanip>
    15. using namespace std;
    16. ostream &myoutput(ostream& stream){
    17. stream<<setw(10)<<setfill('#')<<left;
    18. return stream;
    19. }
    20. int main(){
    21. int i=123;
    22. cout<<i<<endl;
    23. cout<<myoutput<<i<<endl;
    24. return 0;
    25. }
    26. /*
    27. 123
    28. 123#######
    29. */
    30. [例9-11]自定义输入操作函数
    ostream &operator<<(ostream &out, class_name& obj)
    {
        out<<obj.data1;
    ……
        out<<obj.datan;
        return out;
    }
    
    istream &operator>>(istream &in, class_name& obj)
    {
        in>>obj.data1;
        in>>obj.data2;
        ……
        in>>obj.datan;
        return out;
    }
    
    
    #include <iostream>
    using namespace std;
    
    //左移运算符重载
    class Person {
    public:
        int A;
        int B;
        Person(int a = 0, int b = 0) :A(a), B(b) {}
    };
    ostream& operator<<(ostream& out, Person& P) {
        out << P.A << "," << P.B;
        return out;
    }
    
    int main() 
    {
        Person P1(1, 2);
    
        cout << P1 << endl;   //cout为标准的输出流对象
        return 0;
    }
    
    可以使用seekg( )函数和seekp( )函数指定位置访问文件:
    ostream& seekp(streamoff off, ios::seek_dir dir);
    istream& seekg(streamoff off, ios::seek_dir dir);
      函数seekp( )用于输出文件,将相应的文件指针get从dir的位置移动off个字节;
      函数seekg( )用于输入文件,将相应的文件指针dir的位置移动off个字节。  
    
    dir取值如下:
    ios::beg:从文件头开始,此时off取值为正;
    ios::cur:从文件当前位置开始,此时off取值可为正可为负;
    ios::end:从文件末尾开始,此时off取值为负。
    #include <iostream>
    #include <fstream>
    using namespace std;
    void myread(const char* fname)
    {
        ifstream file_read(fname,ios::binary);
        char c[10];
        if(file_read){
            file_read.seekg(1,ios::beg);    //从开始位置移动1位
            file_read.get(c[0]);       //读取一个字符   
             file_read.seekg(2,ios::cur);//从当前位置移动2位
            file_read.get(&c[1],4*sizeof(char));//一次读取1个字符,连续读3次,最后一个放置'\0'
            cout<<c;
        }
    }
    void mywrite(const char* fname){
        char c[30];
        for(int i=0;i<26;i++)
            c[i]='A'+i;
        ofstream file_write(fname,ios::binary);
        if(file_write)
            file_write.write(c,26*sizeof(char));    
    }
    int main()
    {
        char fname[20]="word.file";
        mywrite(fname);
        myread(fname);
        return 0;
    }
    程序运行结果为:
    BEFG