zoukankan      html  css  js  c++  java
  • C++——流类库与输入/输出

    I/O流的概念

    当程序与外界环境进行信息交换时,存在着两个对象,一个是程序中的对象,另一个是文件对象,流是一种抽象,它负责在数据的生产者和数据的消费者之间建立连接,并管理数据的流动。程序建立一个流对象,并指定这个流对象与某个文件对象建立连接,程序操作流对象,流对象通过文件系统对所连接的文件对象产生作用。读操作在流数据抽象中被称为提取,写操作被称为插入。

    1、最重要的三个输出流:ostream(),ofstream(),ostringstream().

    预先定义的输出流对象:cout标准输出;cerr标准错误输出,没有缓冲,发送给它的内容立即被输出。clog类似于cerr,但是有缓冲,缓冲区满时被输出。

    ofstream类支持磁盘文件输出
    如果在构造函数中指定一个文件名,当构造这个文件时该文件是自动打开的

    ofstream myFile("filename",iosmode);
    可以在调用默认构造函数之后使用open成员函数打开文件
    ofstream myFile; //声明一个静态输出文件流对象
    myFile.open("filename",iosmode);    //打开文件,使流对象与文件建立联系
    ofstream* pmyFile = new ofstream;   //建立一个动态的输出文件流对象
    pmyFile->open("filename",iosmode); //打开文件,使流对象与文件建立联系
    插入运算符<<:插入运算符是所有标准数据类型预先设计的,用于传送字节到一个输出流对象。

    控制输出格式

    控制输出宽度:setw操纵符或者调用width成员函数为每个项指定输出宽度;

    #include <iostream>
    using namespace std;
    int main()  
    {
        double values[] = {1.23,35.36,653.7,4358.24};……setw()

                                                     ……char *names[]={"Zoot","Jimmy","Al","Stan"};
        for(int i=0;i<4;i++)                         ……
        {                                            ……cout<<setw(6)<<names[i]<<setw(10)<<values[i]<<endl;
            cout.width(10);                          ……

            cout.fill('*');//用星号填充,体现width的效果……
            cout << values[i] <<' ';                ……
        }                                            ……
    }//输出结果:                                     ……
    ******1.23                                       ……  Zoot      1.23
    *****35.36                                       …… Jimmy     35.36
    *****653.7                                       ……     Al    653.7  
    ***4358.24                                       ……  Stan   4358.24

    控制输出精度和对齐格式setprecision和setiosflags(ios::left(right))

    #include <iostream>  
    #include <iomanip>  
    using namespace std;
    int main()  
    {
        double values[] = {1.23,35.36,653.7,4358.24};
        char *names[] = {"Zoot", "Jimmy", "Al", "Stan"};
        for (int i=0;i<4;i++)
           cout << setiosflags(ios_base::left)
               << setw(6)<< names[i]
               << resetiosflags(ios_base::left)//使得第一次设置失效,因此第二次显示又默认向右对齐了。
               << setw(10)  << setprecision(1)  << values[i]  << endl;
    }//输出结果:
    Zoot           1
    Jimmy     4e+001
    Al        7e+002
    Stan      4e+003

    输出文件流成员函数:是与操作符等价的成员函数,执行非格式化写操作的成员函数,其它修改流状态且不同于操纵符或插入运算符的成员函数。

    open函数 把流与一个特定的磁盘文件关联起来。需要指定打开模式。
    put函数 把一个字符写到输出流中。
    write函数 把内存中的一块内容写到一个输出文件流中
    seekp和tellp函数 操作文件流的内部指针
    close函数 关闭与一个输出文件流关联的磁盘文件
    错误处理函数 在写到一个流时进行错误处理
    #include <fstream>//向文件输出
    using namespace std;
    struct Date
    {
        int mo,da,yr; 
    };
    int main() 
    {
        Date dt = {6,10,92};
        ofstream tfile("date.dat",ios_base::binary);//指定二进制输出,避免换行符的转换。
        tfile.write((char *) &dt,sizeof dt);//首先取dt地址并强制转换为字符类型,作为首地址,且由此,写入dt全部内容。
     tfile.close();
    }

    以通常方式构造一个流,然后使用setmode成员函数,在文件打开后改变模式。
    使用ofstream构造函数中的模式参量指定二进制输出模式
    使用二进制操作符代替setmode成员函数:ofs << binary;
    2、最重要的输入流类:
    istream类最适合用于顺序文本模式输入。cin是其派生类istream_withassign的对象。
    ifstream类支持磁盘文件输入。还有istringstream
    如果在构造函数中指定一个文件名,在构造该对象时该文件便自动打开。
    ifstream myFile("filename",iosmode);
    在调用默认构造函数之后使用open函数来打开文件。
    ifstream myFile;//建立一个文件流对象
    myFile.open("filename",iosmode);  //打开文件"filename"

    提取运算符>>。

    open函数 把该流与一个特定磁盘文件相关联。
    get函数  与提取运算符(>>)很相像,主要的不同点是get函数在读入数据时包括空白字符。

    getline  从输入流中读取多个字符,并且允许指定输入终止字符,读取完成后,从读取内容中删除终止字符。

    read成员函数  从一个文件读字节到一个指定的内存区域,由长度参数确定要读的字节数。如果给出长度参数,当遇到文件结束或者在文本模式文件中遇到文件结束标记字符时结束读取。
    seekg函数  用来设置输入文件流中读取数据位置的指针。
    tellg函数  返回当前文件读指针的位置。
    close函数  关闭与一个输入文件流关联的磁盘文件。

    //从文件读取二进制记录

    #include<iostream>
    #include <fstream>
    #include<cstring>
    using namespace std;
    int main()
    {
        struct
        {
            double salary;
            char name[23];
        } employee1, employee2;
     employee1.salary=8000;
     strcpy(employee1.name, "L Zheng");
     ofstream outfile("payroll",ios_base::binary);
     outfile.write((char *) &employee1,sizeof(employee1));
     outfile.close();
        ifstream is("payroll",ios_base::binary);
        if (is)
           {
                 is.read((char *) &employee2,sizeof(employee2));

    //设置位置指针
    #include<iostream>
    #include <fstream>
    using namespace std;
    int main()
    {
        char ch;
        ifstream tfile("payroll",ios_base::binary);
        if(tfile)
        {
            tfile.seekg(8);
            while (tfile.good())
            {// 遇到文件结束或读取操作失败时结束读操作
                 tfile.get(ch);
                 if (!ch) break;     // 如果没有读到则退出循环
                 cout << ch;
            }
        }
        else
        {
             cout << "ERROR: Cannot open file 'payroll'." << endl;
         }
      tfile.close();
    }

                 cout << employee2.name << ' ' << employee2.salary << endl;
            }
        else
        {
            cout << "ERROR: Cannot open file 'payroll'." << endl;
        }
     is.close();
    }
    //读文件并显示其中空格的位置
    #include<iostream>
    #include <fstream>
    using namespace std;
    int main()
    {
        char ch;
        ifstream tfile("payroll",ios_base::binary);
        if (tfile)
        {
             while (tfile.good())
             {
                 streampos here = tfile.tellg();
                 tfile.get(ch);
                 if (ch==' ')
                      cout << "Position " << here << " is a space ";
             }
        }
        else
             cout << "ERROR: Cannot open file 'payroll'." << endl;
        tfile.close();
    }

  • 相关阅读:
    2020.9.26
    2020.10.2
    判断方法
    sql与include
    File类的获取方法
    【每日日报】第十五天
    【每日日报】第十三天
    【每日日报】第十四天
    两数相加(输入框)
    判断闰年
  • 原文地址:https://www.cnblogs.com/lemaden/p/10238080.html
Copyright © 2011-2022 走看看