C++ 11 深度学习(九)C++文件IO

1.将数据写入文件

    #include <iostream>
    #include <fstream>
    using namespace std;
     
     
    int main()
    {
        ofstream p1;
        p1.open("outfile.txt");
        p1   <<  "向文件写入信息"  <<  endl;
        p1.close();
        return 0;
    }

2.将数据从文件中读出

    #include <iostream>
    #include <fstream>
    using namespace std;
     
    int main()
    {
        char p[40];
        ifstream p1;
        p1.open("outfile.txt");
        p1 >> p;
        cout  <<  p <<  endl;
        p1.close();
        return 0;
    }

3.格式化输出读取一行

    #include <iostream>
    #include <fstream>
    using namespace std;
     
    int main()
    {
        char p[40];
        ifstream p1;
        p1.open("outfile.txt");
        if (p1.fail())
        {
            return 0;
        }
        while (!p1.eof())
        {
            //读取一行,以#为分隔
            p1.getline(p, 40, '#');
            cout << p << endl;
        }
        
        p1.close();
        return 0;
    }

4.字符操作

   instream.get( )获取一个字符

   outstream.put( )写入一个字符
5.文件打开模式

 
6.流状态位

clear函数作用是所有标志位置为0.
7.二进制读写

    在文本模式对整数199进行存储的时候,实际是对199的ASCII码方式'1','9',9'   -----    0x31   0x39   0x39  的方式进行存储的,占用3Byte,文本模式是建立在二进制读写的基础之上,只不过是将二进制信息进行了(字符编解码)。
    如果以二进制进行存储的话占用了1Byte,使用十六进制C7表示,把int型转换成十六进制进行存储。

8.将任意类型数据写入文件

 把数据需要转换为字节序列,即字节流;可以使用  reinterpret_cast(address)

1.写入操作

    #include <iostream>
    #include <armadillo>
    #include <fstream>
    using namespace std;
    using namespace arma;
     
    int main()
    {
        fstream binaryio;
        binaryio.open("city.dat", ios::out | ios::binary);
        int value = 199;
        binaryio.write(reinterpret_cast<char*>(&value), sizeof(value));
        binaryio.close();
     
        cout << "Done" << endl;
     
        return 0;
    }

2.读取操作

    #include <iostream>
    #include <armadillo>
    #include <fstream>
    using namespace std;
    using namespace arma;
     
    int main()
    {
        fstream binaryio;
        binaryio.open("city.dat", ios::in | ios::binary);
        int value;
        binaryio.read(reinterpret_cast<char*>(&value), sizeof(value));
        binaryio.close();
     
        cout << value << endl;
     
        return 0;
    }

3.二进制写入与读取数组

    #include <iostream>
    #include <armadillo>
    #include <fstream>
    using namespace std;
    using namespace arma;
     
    #define SIZE 5
    int main()
    {
     
        //二进制写入数组
        double array[SIZE] = { 3.4 , 1.3 , 2.5 , 5.66 , 6.9 };
        fstream binaryio;
        binaryio.open("city.dat", ios::out | ios::binary);
        binaryio.write(reinterpret_cast<char*>(&array), sizeof(array));
        binaryio.close();
     
        //二进制读取数组
        double result[SIZE];
        binaryio.open("city.dat", ios::in | ios::binary);
        binaryio.read(reinterpret_cast<char*>(&result), sizeof(array));
        binaryio.close();
     
        for (int i = 0; i < SIZE; i++)
        {
            cout << result[i] << endl;
        }
     
        return 0;
    }

4.当将对象存储到文件中的时候,只存储数据域,而不存储函数域。
5.随机访问文件方法


————————————————
版权声明:本文为CSDN博主「༄yi笑奈何」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_40179458/article/details/112603335

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值