C++语言基础 例程 二进制文件及其顺序读写

简介: 贺老师的教学链接  本课讲解对比ASCII文件和二进制文件//将short int x=12345写入文本文件#include <iostream>#include <fstream>#include <cstdlib>using namespace std;int main( ){ short int x=12345; o

贺老师的教学链接  本课讲解


对比ASCII文件和二进制文件

//将short int x=12345写入文本文件
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main( )
{
    short int x=12345;
    ofstream outfile("binary.dat");

    if(!outfile)
    {
        cerr<<"open error!"<<endl;
        exit(1);//退出程序
    }
    outfile<<x<<endl;
    outfile.close( );
    return 0;
}

//将short int x=12345写入二进制文件
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main( )
{
    short int x=12345;
    ofstream outfile("binary.dat",ios::binary);
    if(!outfile)
    {
        cerr<<"open error!"<<endl;
        exit(1);//退出程序
    }
    outfile.write((char*)&x,2);
    outfile.close( );
    return 0;
}

将数据以二进制形式存放在磁盘文件中
#include<iostream>
#include <fstream>
#include<cstdlib>
using namespace std;
struct student
{
    char name[5];
    int num;
    int age;
    char sex;
};


int main( )
{
    student stud[3]=
    {
        {"Li",25,18,'f'},
        {"Fun",32,19,'m'},
        {"Wang",40,17,'f'}
    };
    ofstream outfile("stud.dat",ios::binary);
    if(!outfile)
    {
        cerr<<"open error!"<<endl;
        exit(1);//退出程序
    }
    for(int i=0; i<3; i++)
        outfile.write((char*)&stud[i],sizeof(stud[i]));
    cout<<"任务完成,请查看文件。"<<endl;
    outfile.close( );
    return 0;
}


将二进制文件中的数据读入内存
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
struct student
{
    char name[5];
    int num;
    int age;
    char sex;
};
int main( )
{
    student stud[3];
    int i;
    ifstream infile("stud.dat",ios::binary);
    if(!infile)
    {
        cerr<<"open error!"<<endl;
        abort( );
    }
    for(i=0; i<3; i++)
        infile.read((char*)&stud[i],sizeof(stud[i]));
    infile.close( );
    for(i=0; i<3; i++)
    {
        cout<<"NO."<<i+1<<endl;
        cout<<"name:"<<stud[i].name<<endl;
        cout<<"num:"<<stud[i].num<<endl;;
        cout<<"age:"<<stud[i].age<<endl;
        cout<<"sex:"<<stud[i].sex<<endl<<endl;
    }
    return 0;
}


目录
相关文章
|
1月前
|
算法 编译器 C语言
C++语言的“Hello World”
C++语言的“Hello World”
14 0
|
1月前
|
编译器 C++
C++语言中const的用法
C++语言中const的用法
13 0
|
1月前
|
存储 编译器 C++
在C++语言中计算并打印出两个数的求和
在C++语言中计算并打印出两个数的求和
22 0
|
1月前
|
C++
C++语言中流程控制
C++语言中流程控制
14 0
|
30天前
|
算法 网络协议 编译器
【C++ 14 新特性】C++14二进制字面量:深度探索与实践
【C++ 14 新特性】C++14二进制字面量:深度探索与实践
38 1
|
7天前
|
缓存 编译器 API
NumPy与其他语言(如C/C++)的接口实践
【4月更文挑战第17天】本文介绍了NumPy与C/C++的接口实践,包括Python与C/C++交互基础、NumPy的C API和Cython的使用。通过案例展示了如何将C++函数与NumPy数组结合,强调了内存管理、类型匹配、错误处理和性能优化的最佳实践。掌握这些技能对于跨语言交互和集成至关重要。
|
16天前
|
存储 C++ iOS开发
C++文件操作(文本文件的读写+二进制文件的读写)
C++文件操作(文本文件的读写+二进制文件的读写)
|
16天前
|
程序员 C++
C++语言模板学习应用案例
C++模板实现通用代码,以适应多种数据类型。示例展示了一个计算两数之和的模板函数`add&lt;T&gt;`,可处理整数和浮点数。在`main`函数中,展示了对`add`模板的调用,分别计算整数和浮点数的和,输出结果。
12 2
|
30天前
|
Java API 开发工具
【软件设计师备考 专题 】C、C++、Java、Visual Basic、Visual C++等语言的基础知识和应用(三)
【软件设计师备考 专题 】C、C++、Java、Visual Basic、Visual C++等语言的基础知识和应用
30 0
|
30天前
|
Java 数据处理 数据库
【软件设计师备考 专题 】C、C++、Java、Visual Basic、Visual C++等语言的基础知识和应用(二)
【软件设计师备考 专题 】C、C++、Java、Visual Basic、Visual C++等语言的基础知识和应用
34 0

热门文章

最新文章