• C++第14课 IO流


    1.流的概念:若干个字节组成的一个字节序列,代表信息从源到目的流动

    流类体系:用类实现的所有流操作
    IO流
    标准输入输出流
    字符流
    文件流
    ios
    istream ostream --->iostream
    strstream
    ifstream ofsteram --->fstream

    /*
        No.1 输入流和输出流的对象
        cin:    标准输入   重定向
        cout:   标准输出   重定向
        cerr:   标准错误   不可重定向
        clog:   标准错误   可以重定向
        //freopen
        No.2 流对象常用的函数,字符和字符串上面的操作
        put(): 输出字符  输入: get()
        write(): 输出字符串 getline()输入字符串
        No.3 C++格式控制
            3.1 包含iomanip
        成员函数的方式                关键字的方式
                                        setbase(n): 设置为n进制
                                        setfill(n): 设置填充字符
        precision                        setprecision(n) :设置精度
        width                            setw(n):   设置宽度
                                        setiosflags(ios::scientific);
                                        setiosflags(ios::left); ios::right
    */
    
    void testIOStream() 
    {
        cerr << "错误" << endl;
        clog << "错误" << endl;
        char str[20] = "";
        cin.getline(str, 20);    //用回车当做数据间隔的
        cout.write(str, 20);
        cout << endl;
        cin >> str;                //输入是以空格作为数据间隔
        cout << str << endl;
        cout << "请输入一个字符:" << endl;
        //cin.clear();
        setbuf(stdin, nullptr);
        str[0] = cin.get();
        cout.put(str[0]);
    }
    int main() 
    {
        cout<<setbase(16) << 32 << endl;
        cout << hex << 33 << endl;
        cout << oct << 32 << endl;
        cout << "ILoveyou" << endl;
        cout << setiosflags(ios::left) <<setw(8) << "姓名" << setw(8) << "年龄" << setw(8) << "编号" << endl;
        //精度不是指小数位,总共打印数据的长度
        cout << setprecision(4) << 233.131314 << endl;
        //设置小数位的精度
        cout <<fixed<< setprecision(4) << 233.131314 << endl;
        //cout.width(10);
        bool bNum = 101;
        cout << boolalpha << bNum << endl;
    
    
        return 0;
    }

    2.move用法

    void print(const char* str)
    {
        cout << str << endl;
    }
    void printRightValue(int&& num) 
    {
        cout << num << endl;
    }
    void printValue(int& num) 
    {
        cout << num++;
    }
    
    int main() 
    {
        print("ILove");
        char name[20] = "dasd";
        print(name);
        printRightValue(1);
        int leftNum = 0;
        int&& xx = 12;
        printRightValue(move(leftNum));
        printValue(ref(leftNum));
        return 0;
    }

    3.字符流

    /*
        No.1 包含 sstream头文件
        No.2 简单看sstream
        sstream : istringstream ostringstream stringstream
        stringstream这个类使我们一般写代码使用的类
        No.3 stringstream一些方法
        string str();  
        void str(const string& str);  重置
    
    */
    int main() 
    {
        //1.基本操作
        stringstream object("string");
        cout << object.str() << endl;
        object.str("ILoveyou");
        cout << object.str() << endl;
        //2.用的多情况用来做类型转换或者字符切割
        cout << to_string(1234) << endl;    //把数字转换为相应的字符串
        stringstream data("");
        int num = 12345;
        data << num;
        char result[20] = "";
        data >> result;
        cout << result;
        cout << endl;
        //切割
        stringstream ip("ip地址: 192.168.1.1");
        char info[20];
        int ipNum[4];
        ip >> info;            //ip地址:
        for (int i = 0; i < 4; i++) 
        {
            char key;
            ip >> ipNum[i];
            if (i != 3) 
            {
                ip >> key;
            }
        }
        for (int i = 0; i < 4; i++) 
        {
            cout << ipNum[i] << "	";
        }
        cout << endl;
        return 0;
    }

    4.文件流

    /*
        No.1 流类
            ofstream :打开文件写操作
            ifstream :打开文件读操作
        fstream : 可读可写
        No.2 包含头文件: fstream
        No.3 文件的基本操作
        3.1 打开文件
        void open(const char* URL,int mode);
        mode: 打开方式
            ios::in   读的方式打开文件
            ios::out  写的方式打开文件
            ios::app  追加的方式写文件
            ios::ate  打开已有文件,文件指针在文件末尾
            ios::trunc  文件不存在的情况具有创建,   ios::trunc|ios::in
            ios::binary 二进制形式
            ios::nocreate 不创建
            ios::noreplace 不替换
            ios::out|ios::in  可读可写方式打开文件
        判断文件打开是否成功
            1.is_open()的返回值判断是成功 返回true表示打开成功
            2.!文件对象;
        3.2 读写文件
        3.3 关闭文件
            close()关闭文件
    
    */
    void testFileOpen() 
    {
        //1.构造的时候直接打开文件
        fstream fp("1.txt");
        if (!fp)
        {
            cout << "文件打开失败!" << endl;
        }
        cout << boolalpha << fp.is_open() << endl;
        fp.close();
        //2.调用打开文件
        fstream read;
        read.open("2.txt", ios::in | ios::out | ios::trunc);
        if (!read.is_open())
        {
            cout << "文件打开失败!" << endl;
        }
        read.close();
    }
    
    //1.流的方式读写
    void testRWFileByStream() 
    {
        fstream fp("testRW.txt", ios::in | ios::out | ios::trunc);
        fp << "姓名" << "	" << "年龄";
        fp.close();
        fstream fp2("testRW.txt", ios::in | ios::out );
        char name[20];
        char age[20];
        fp2 >> name >> age;
        cout << name << "	" << age << endl;
        fp2.close();
    }
    //2.字符流的方式: get put
    void  asciiRWFile() 
    {
        fstream read("ASCII.txt", ios::in);
        fstream write("ASCII2.txt", ios::out);
        while (!read.eof()) 
        {
            char key;
            read.get(key);
            write.put(key);
        }
        read.close();
        write.close();
    }
    //3.二进制读写
    //write read函数
    void binaryRWFile()
    {
        fstream rFp("test.txt", ios::in|ios::binary);
        fstream wFp("test2.txt", ios::out|ios::binary);
        while (!rFp.eof()) 
        {
            char str[8] = "";
            rFp.read(str, 8);
            wFp.write(str, 8);
        }
        rFp.close();
        wFp.close();
    }
    
    //No.4 文件指针移动
    void testFileMove() 
    {
        /*
            ifstream
                seekg(long int pos);
                seekg(long int pos,ios_base::seekdir begin);
            ofstream
                seekp(long int pos)
                seekp(long int pos, ios_base::seekdir begin);
            begin:
                ios::beg   开始
                ios::end   结束
                ios::cur   当前
        */
        fstream  fp("move.txt", ios::in);
        if (!fp) 
        {
            cerr << "file error" << endl;
        }
        fp.seekg(3);
        char key = fp.get();
        cout << key << endl;
        fp.seekg(0, ios::beg);
        key = fp.get();
        cout << key << endl;
        fp.close();
    }
  • 相关阅读:
    补:冲刺Day1
    需求规格说明书2.0
    补:冲刺Day2
    冲刺Day3
    Base64上传图片
    C#怎样通过url调用接口
    js键盘控制DIV移动
    利用MVC的过滤器实现url的参数加密和解密
    在.net MVC中异步上传图片或者文件
    mvc url路由参数的加密和解密
  • 原文地址:https://www.cnblogs.com/creature-lurk/p/15255621.html
Copyright © 2020-2023  润新知