二进制文件读写

简介:  1.二进制写入,ios::binary #include<iostream> #include <fstream> #include <string>   using namespace std;   struct MyStruct {     char *p = "北京是


1.二进制写入,ios::binary

#include<iostream>

#include <fstream>

#include <string>

 

using namespace std;

 

struct MyStruct

{

    char *p = "北京是帝都";

    int num = 20;

    double db = 10.98;

    char ch = 'a';

};

 

void main1()

{

    ofstream fout("E:\\wen.txt", ios::out);

    ifstream fin("E:\\wen.txt");

    //下面的大小时24

    std::cout << sizeof(MyStruct) << std::endl;

    MyStruct my1;

    //向文件中写入内容

    fout << my1.p << " " << my1.num << " " << my1.db << " " << my1.ch << "\n";

    fout.close();

    char str[100] = { 0 };

    //提取文件内容

    fin.getline(str, 100, 0);

    std::cout << str << std::endl;

    fin.close();

    cin.get();

    //运行结果是:

    //24

    //北京是帝都 20 10.98 a

    //同时在E盘生成了wen.txt

}

 

void main()

{

    MyStruct my1;

    my1.p = "chuheridangwu";

    //写入文件,按照二进制的方式写入,要加上:ios::binary

    ofstream fout("C:\\bin.bin", ios::binary);

    fout.write((char *)&my1, sizeof(my1));

    //第一个参数是要写入文件的内存的首地址

    //第二个参数是长度

    fout.close();

    ifstream fin("C:\\bin.bin", ios::binary);

    MyStruct newmy1;

    fin.read((char *)&newmy1, sizeof(newmy1));

    //保存文件读取到内存,内存首地址

    //长度

    std::cout << newmy1.p << std::endl;

    fin.close();

 

    std::cin.get();

//这里的运行结果是:chuheridangwu

}

2.字节的二进制

#include<iostream>

#include <fstream>

#include <string>

 

using namespace std;

 

void main()

{

    ifstream fin("C:\\write.exe", ios::binary);

    ofstream fout("C:\\newwrite.exe", ios::binary);

    if (!fin || !fout)

    {

        std::cout << "文件打开失败";

        return;

    }

    std::cout << "文件拷贝开始\n";

    char ch = 0;

    while (fout && fin.get(ch))//引用的方式读取到一个字符

    {

        fout.put(ch);//写入一个字节

 

    }

    fin.close();

    fout.close();

 

    std::cout << "文件拷贝完成";

    std::cin.get();

}

 

3.二进制内存文件拷贝

#include<iostream>

#include <fstream>

#include <string>

 

using namespace std;

 

struct MyStruct

{

    int i;

    double db;

};

 

void main()

{

    ofstream fout("E:\\big.txt", ios::binary);

    MyStruct ss[5] = { { 1, 1.1 }, { 2, 2.2 }, { 3, 3.3 }, { 4, 4.4 }, {5, 5.5 } };

    fout.write((char *)ss, sizeof(MyStruct)* 5);

    fout.close();

 

    ifstream fin("E:\\big.txt", ios::binary);

    MyStruct *p = new MyStruct[5];//动态分配内存

    fin.read((char *)p, sizeof(MyStruct)* 5);

    fin.close();

 

    for (int i = 0; i < 5; i++)

    {

        std::cout << p[i].i << " " << p[i].db << std::endl;

    }

 

    cin.get();

}

运行结果:

3.随机二进制文件读写

#include<iostream>

#include <fstream>

using namespace std;

 

void main1a()

{

    double db[10] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9,10.1 };

    ofstream  fout("N.txt", ios::binary);

    fout.write((char *)db, 80);

    fout.close();

    double *p = new double[10];

    ifstream fin("N.txt", ios::binary);

 

    fin.read((char *)p, 80);

    fin.close();

    for (int i = 0; i < 10; i++)

    {

        std::cout << p[i] << endl;

    }

 

    std::cin.get();

}

 

void main2b()//随机位置读取

{

    double db[10] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9,10.1 };

    ofstream  fout("N.txt", ios::binary);

    fout.write((char *)db, 80);

    fout.close();

    double *p = new double[5];

    ifstream fin("N.txt", ios::binary);

    fin.seekg(-40, ios::end);//读写

    fin.read((char *)p, 40);

    fin.close();

    for (int i = 0; i < 5; i++)

    {

        std::cout << p[i] << endl;

    }

 

    std::cin.get();

}

 

 

void main()

{

    //先读取

    double *p = new double[10];

    ifstream fin("N.txt", ios::binary);

    fin.read((char *)p, 80);

    fin.close();

    for (int i = 0; i < 10; i++)

    {

        std::cout << p[i] << endl;

    }

    //随机写入

    double db[] = { 100.9, 200.8, 300.7, 400.6, 500.5 };

    //ios::in | ios::out不再清零文件,否则会清零

    ofstream  fout("N.txt", ios::binary | ios::in | ios::out);

    //fout.seekp(40, ios::beg);

    fout.seekp(0, ios::end);//写入

    fout.write((char *)db, 40);

    fout.close();

 

    //再次读取

    {

        double *p = new double[20];

        ifstream fin("N.txt", ios::binary);

        fin.read((char *)p, 160);

        fin.close();

        for (int i = 0; i < 20; i++)

        {

            std::cout << p[i] << endl;

        }

    }

 

    std::cin.get();

}

4.随机文件读写

#include<iostream>

#include <fstream>

using namespace std;

 

void main1()//随机位置读取

{

    ofstream fout("p.txt");

    fout << "1234567890abcdefghijklmn";

    fout.close();

    ifstream fin("p.txt");

    //fin.seekg(9, ios::beg);//从开始,往前9个字符

    fin.seekg(-9, ios::end);//从尾部,倒数9个字符

    char ch;

    while (fin.get(ch))

    {

        cout << ch;

    }

    fin.close();

    cin.get();

}

 

void main2()

{

    ofstream fout("px.txt");

    fout << "1234567890abcdefghijklmn";

    fout.close();

 

    ofstream Fout("px.txt", ios::in | ios::out);

    char str[] = "ABCDEFG";

    Fout.seekp(0, ios::end);//随机位置进行读写

    long size = Fout.tellp();//当前位置距离begin有多少个字节,尾部可以获取文件大小

 

    cout << size << endl;

    Fout << str;

    Fout.close();

 

    ifstream fin("px.txt");

    char ch;

    while (fin.get(ch))

    {

        cout << ch;

    }

 

    fin.close();

    cin.get();

}

 

 

 

目录
相关文章
|
4月前
|
人工智能 BI
文件的读写
文件的读写。
21 0
|
15天前
|
存储 C++ iOS开发
C++文件操作(文本文件的读写+二进制文件的读写)
C++文件操作(文本文件的读写+二进制文件的读写)
|
3月前
|
存储 JSON Go
纯文本文件的读写操作详解
纯文本文件的读写操作详解
38 0
|
5月前
|
存储 Unix 编译器
32 QT - 二进制文件读写
32 QT - 二进制文件读写
37 0
|
iOS开发 C++
C++文件读写操作分析文本文件与二进制文件
文本文件 写文件 写文件步骤如下: 1. 包含头文件 #include <fstream> 2. 创建流对象 ofstream ofs; 3. 打开文件 ofs.open("文件路径",打开方式); 4. 写数据 ofs << "写入的数据"; 5. 关闭文件 ofs.close(); 文件打开方式: 打开方式 解释 ios::in 为读文件而打开文件 ios::out 为写文件而打开文件 ios::ate 初始位置:文件尾 ios::app 追加方式写文件 ios::trunc 如果文件存在先删除,再创建 ios::binary 二进制方式
342 0
C++文件读写操作分析文本文件与二进制文件
|
SQL Oracle 关系型数据库
jdbc读写二进制文件
jdbc读写二进制文件
100 0
|
C#
原 BinaryWriter和BinaryReader(二进制文件的读写)
原文 BinaryWriter和BinaryReader(二进制文件的读写) C#的FileStream类提供了最原始的字节级上的文件读写功能,但我们习惯于对字符串操作,于是StreamWriter和 StreamReader类增强了FileStream,它让我们在字符串级别上操作文件,但有的时候我们还是需要在字节级上操作文件,却又不是一个字节 一个字节的操作,通常是2个、4个或8个字节这样操作,这便有了BinaryWriter和BinaryReader类,它们可以将一个字符或数字按指定 个数字节写入,也可以一次读取指定个数字节转为字符或数字。
1557 0
|
存储 数据库

热门文章

最新文章