C++中文件的随机读写方法及其作用介绍

发布时间:2021-09-02 23:03:15 作者:chen
来源:亿速云 阅读:355

这篇文章主要介绍“C++中文件的随机读写方法及其作用介绍”,在日常操作中,相信很多人在C++中文件的随机读写方法及其作用介绍问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C++中文件的随机读写方法及其作用介绍”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

目录

概述

文件的操作方式分为顺序读写和随机读写. 顺序读写指文件的指针只能从头移到尾巴. 随机读写指文件指针可以随意移动, 根据需要.

随机读写

文件指针: 在磁盘文件操作中有一个文件指针, 用来指明进行读写的位置.

函数

文件流提供了一些有关文件指针的成员函数:

成员函数作用
gcount()返回最后一次输入所读入的字节数
tellg()返回输入文件指针的当前位置
seekg (文件中的位置)将输入文件中指针移到指定的位置
seekg (位移量, 参照位置)参照位置为基础移动到指定的位置
tellp()返回输出文件指针当前的位置
seekp (文件中的位置)将输出文件中指针移到指定的位置
seekp (位移量, 参照位置)以参照位置为基础移动若干字节

例子

从键盘输入 10 个整数, 并将其保存到数据文件 f1. dat 中, 再从文件中将数据读出来, 显示在屏幕上.

#include <fstream>
#include <iostream>
using namespace std;

int main() {
    int a[10], b[10];
    
    // 打开文件
    fstream iofile("temp.txt", ios::in | ios::out);
    if(!iofile) {
        cerr << "open error!" << endl;
        exit(1);
    }
    
    // 写入文件
    cout << "enter 10 integer numbers:\n";
    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] << " ";
    }
    iofile.close();

    return 0;
}

输出结果:

enter 10 integer numbers:
1 2 3 4 5 6 7 8 9 10
The numbers have been writen to file.
Display the data by read from file:
1 2 3 4 5 6 7 8 9 10

指针流成员函数

文件中的位置和位移量为long型, 以字节为单位.
参照位置可以是下面三者之一:

用法: 以 seekg (位移量, 参照位置) 为例:

随机访问二进制数据

利用成员函数移动指针, 随机地访问二进制数据文件中任意一位置上的数据, 还可以修改文件中的内容.

学生数据处理:

#include <fstream>
#include <iostream>
#include "Student.h"
using namespace std;

int main() {
    // 打开文件
    fstream iofile("student.txt",ios::in|ios::out);
    if(!iofile) {
        cerr << "open error!" << endl;
        exit(1);
    }

    // 向磁盘文件输出2个学生的数据
    Student stud[2]{
            {1, "Little White"},
            {2, "Big White"}
    };

    for (int i = 0; i < 2; ++i) {
        iofile.write((char *) &stud[i], sizeof(stud[i]));
    }
    // 读取学生数据,并显示
    Student read[2];
    for (int i = 0; i < 2; ++i) {
        iofile.seekg(i*sizeof(stud[i]),ios::beg);
        iofile.read((char *)&read[i],sizeof(read[0]));
    }

    // 修改第2个学生的数据后存回文件原位置
    stud[1].setId(1012); //修改
    stud[1].setName("Wu");
    iofile.seekp(sizeof(stud[0]),ios::beg);
    iofile.write((char *)&stud[1],sizeof(stud[2]));
    iofile.seekg(0,ios::beg);


    // 读入修改后的2个学生的数据并显示出来
    for(int i=0; i<2; i++)
    {
        iofile.read((char *)&stud[i],sizeof(stud[i]));
        stud[i].display();
    }
    iofile.close( );
    return 0;
}

输出结果:

id= 1
name= Little White
id= 1012
name= Wu

到此,关于“C++中文件的随机读写方法及其作用介绍”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. Java 实现文件随机读写-RandomAccessFile
  2. C++中读写txt文件并分离字符的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++

上一篇:Android怎么实现背景颜色滑动渐变效果

下一篇:MySQL中的隐藏列的具体查看方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》