C++语言基础 例程 文件的随机读写

简介: 贺老师的教学链接  本课讲解示例:写到尾再从头读#include <iostream>#include <fstream>#include <cstdlib>using namespace std;int main( ){ int a[10], b[10]; fstream iofile("f1.dat",ios::in|io

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


示例:写到尾再从头读

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main( )
{
    int a[10], b[10];
    fstream iofile("f1.dat",ios::in|ios::out);
    if(!iofile)
    {
        cerr<<"open error!"<<endl;
        exit(1);
    }
    cout<<"enter 10 integer numbers:"<<endl;
    for(int i=0; i<10; i++)
    {
        cin>>a[i];
        iofile<<a[i]<<" ";
    }
    cout<<"The numbers have been writen to file. "<<endl;
    cout<<"Display the data by read from file: "<<endl;
    iofile.seekg(0, ios::beg);  //文件指针重回文件开始位置
    for(int i=0; i<10; i++)
    {
        iofile>>b[i];
        cout<<b[i]<<" ";
    }
    cout<<endl;
    iofile.close();
    return 0;
}


学生数据处理
#include<iostream>
#include <fstream>
#include<cstdlib>
#include<cstring>
using namespace std;
struct student
{
    int num;
    char name[20];
    float score;
};
int main( )
{
    student stud[5]= {{1001,"Li",85},{1002,"Fun",97.5},{1004,"Wang",54},
        {1006,"Tan",76.5},{1010,"ling",96}
    };
    fstream iofile("stud.dat",ios::in|ios::out|ios::binary);
    //用fstream类定义输入输出二进制文件流对象iofile
    if(!iofile)
    {
        cerr<<"open error!"<<endl;
        abort( );
    }
    //(1)向磁盘文件输出5个学生的数据并显示出来
    cout<<"(1)向磁盘文件输出5个学生的数据并显示出来"<<endl;
    for(int i=0; i<5; i++)
    {
        iofile.write((char *)&stud[i],sizeof(stud[i]));
        cout<<stud[i].num<<" "<<stud[i].name<<" "<<stud[i].score<<endl;
    }


    //(2)将磁盘文件中的第1,3,5个学生数据读入程序,并显示出来;
    cout<<"(2)将磁盘文件中的第1,3,5个学生数据读入程序,并显示出来"<<endl;
    student stud1[5];                  //用来存放从磁盘文件读入的数据
    for(int i=0; i<5; i=i+2)
    {
        iofile.seekg(i*sizeof(stud[i]),ios::beg);  //定位于第0,2,4学生数据开头
        iofile.read((char *)&stud1[i/2],sizeof(stud1[0]));
        //先后读入3个学生的数据,存放在stud1[0],stud[1]和stud[2]中
        cout<<stud1[i/2].num<<" "<<stud1[i/2].name<<" "<<stud1[i/2].score<<endl;
        //输出stud1[0],stud[1]和stud[2]各成员的值
    }
    cout<<endl;


    //(3) 将第3个学生的数据修改后存回磁盘文件中的原有位置。
    cout<<"(3)将第3个学生的数据修改后存回磁盘文件中的原有位置"<<endl;
    stud[2].num=1012;                         //修改第3个学生(序号为2)的数据
    strcpy(stud[2].name,"Wu");
    stud[2].score=60;
    iofile.seekp(2*sizeof(stud[0]),ios::beg);   //定位于第3个学生数据的开头
    iofile.write((char *)&stud[2],sizeof(stud[2])); //更新第3个学生数据
    iofile.seekg(0,ios::beg);                       //重新定位于文件开头


    //(4)从磁盘文件读入修改后的5个学生的数据并显示出来。
    cout<<"(4)从磁盘文件读入修改后的5个学生的数据并显示出来"<<endl;
    for(int i=0; i<5; i++)
    {
        iofile.read((char *)&stud[i],sizeof(stud[i]));  //读入5个学生的数据
        cout<<stud[i].num<<" "<<stud[i].name<<" "<<stud[i].score<<endl;
    }
    iofile.close( );
    return 0;
}


学生数据处理(OO版)
#include<iostream>
#include <fstream>
#include<cstdlib>
#include<cstring>
using namespace std;
class Student
{
public:
    Student(void) {}
    Student(int n, char nam[20], float s):num(n),score(s)
    {
        strcpy(name,nam);
    }
    void setNum(int n)
    {
        num=n;
    }
    void setName(char nam[20])
    {
        strcpy(name,nam);
    }
    void setScore(float s)
    {
        score=s;
    }
    void show()
    {
        cout<<num<<" "<<name<<" "<<score<<endl;    //显示通过<<的重载实现更自然
    }
private:
    int num;
    char name[20];
    float score;
};


int main( )
{
    Student stud[5]=
    {
        Student(1001,"Li",85),
        Student(1002,"Fun",97.5),
        Student(1004,"Wang",54),
        Student(1006,"Tan",76.5),
        Student(1010,"ling",96)
    };
    fstream iofile("stud.dat",ios::in|ios::out|ios::binary);


    //用fstream类定义输入输出二进制文件流对象iofile
    if(!iofile)
    {
        cerr<<"open error!"<<endl;
        abort( );
    }
    //(1)向磁盘文件输出5个学生的数据并显示出来
    cout<<"(1)向磁盘文件输出5个学生的数据并显示出来"<<endl;
    for(int i=0; i<5; i++)
    {
        iofile.write((char *)&stud[i],sizeof(stud[i]));
        stud[i].show();
    }


    //(2)将磁盘文件中的第1,3,5个学生数据读入程序,并显示出来;
    cout<<"(2)将磁盘文件中的第1,3,5个学生数据读入程序,并显示出来"<<endl;
    Student stud1[5];                  //用来存放从磁盘文件读入的数据
    for(int i=0; i<5; i=i+2)
    {
        iofile.seekg(i*sizeof(stud[i]),ios::beg);  //定位于第0,2,4学生数据开头
        iofile.read((char *)&stud1[i/2],sizeof(stud1[0]));  //先后读入3个学生的数据,存放在stud1[0],stud[1]和stud[2]中
        stud1[i/2].show();		//输出stud1[0],stud[1]和stud[2]各成员的值
    }
    cout<<endl;


    //(3) 将第3个学生的数据修改后存回磁盘文件中的原有位置。
    cout<<"(3)将第3个学生的数据修改后存回磁盘文件中的原有位置"<<endl;
    stud[2].setNum(1012);                         //修改第3个学生(序号为2)的数据
    stud[2].setName("Wu");
    stud[2].setScore(60);
    iofile.seekp(2*sizeof(stud[0]),ios::beg);   //定位于第3个学生数据的开头
    iofile.write((char *)&stud[2],sizeof(stud[2])); //更新第3个学生数据
    iofile.seekg(0,ios::beg);                       //重新定位于文件开头


    //(4)从磁盘文件读入修改后的5个学生的数据并显示出来。
    cout<<"(4)从磁盘文件读入修改后的5个学生的数据并显示出来"<<endl;
    for(int i=0; i<5; i++)
    {
        iofile.read((char *)&stud[i],sizeof(stud[i]));  //读入5个学生的数据
       stud[i].show();
    }
    iofile.close( );
    return 0;
}


目录
相关文章
|
21天前
|
存储 C++
基于C++的简易文件压缩与解压缩工具设计与实现
基于C++的简易文件压缩与解压缩工具设计与实现
12 3
|
23天前
|
安全 算法 程序员
【C/C++ 文件操作】深入理解C语言中的文件锁定机制
【C/C++ 文件操作】深入理解C语言中的文件锁定机制
31 0
|
27天前
|
Unix 编译器 Linux
【计算机基础 ELF文件】深入探索ELF文件:C++编程中的关键组成部分
【计算机基础 ELF文件】深入探索ELF文件:C++编程中的关键组成部分
46 0
|
24天前
|
Linux C++ iOS开发
【C++ 17 新特性 文件管理】探索C++ Filesystem库:文件和目录操作的全面指南(二)
【C++ 17 新特性 文件管理】探索C++ Filesystem库:文件和目录操作的全面指南
237 2
|
24天前
|
Linux API C++
【C++ 17 新特性 文件管理】探索C++ Filesystem库:文件和目录操作的全面指南(一)
【C++ 17 新特性 文件管理】探索C++ Filesystem库:文件和目录操作的全面指南
293 2
|
1天前
|
缓存 编译器 API
NumPy与其他语言(如C/C++)的接口实践
【4月更文挑战第17天】本文介绍了NumPy与C/C++的接口实践,包括Python与C/C++交互基础、NumPy的C API和Cython的使用。通过案例展示了如何将C++函数与NumPy数组结合,强调了内存管理、类型匹配、错误处理和性能优化的最佳实践。掌握这些技能对于跨语言交互和集成至关重要。
|
10天前
|
程序员 C++
C++语言模板学习应用案例
C++模板实现通用代码,以适应多种数据类型。示例展示了一个计算两数之和的模板函数`add&lt;T&gt;`,可处理整数和浮点数。在`main`函数中,展示了对`add`模板的调用,分别计算整数和浮点数的和,输出结果。
10 2
|
16天前
|
C++ Python
【C++/Python】C++调用python文件
【C++/Python】C++调用python文件
|
24天前
|
Java API 开发工具
【软件设计师备考 专题 】C、C++、Java、Visual Basic、Visual C++等语言的基础知识和应用(三)
【软件设计师备考 专题 】C、C++、Java、Visual Basic、Visual C++等语言的基础知识和应用
30 0
|
24天前
|
Java 数据处理 数据库
【软件设计师备考 专题 】C、C++、Java、Visual Basic、Visual C++等语言的基础知识和应用(二)
【软件设计师备考 专题 】C、C++、Java、Visual Basic、Visual C++等语言的基础知识和应用
34 0