C++学习笔记之对文件的操作<2>

本文详细介绍了C++中使用ofstream和ifstream类进行文件操作的方法,包括文件的打开、关闭、读写,以及二进制和文本文件的区别。通过实例演示了如何在文件末尾追加数据,以及如何以二进制模式读写结构体。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载自**

https://www.cnblogs.com/uniqueliu/archive/2011/08/03/2126680.html

**
什么都不说了,继续《C++学习笔记之对文件的操作<1>》的内容…

=功能展示

打开文件的方式
当我们想要打开的文件不存在的时候,一般地,ofstream类的对象会默认地自动创建一个文件。而如果我们想要打开的文件是存在的,那么就会调用ofstream的构造函数或者是调用open()函数进行打开。下面,我们来看一下MSDN上面是如何定义open()函数的:

首先是函数原型:

void open(
    const char *_Filename,
    ios_base::openmode _Mode = ios_base::in | ios_base::out,
    int _Prot = (int)ios_base::_Openprot
);
void open(
    const char *_Filename,
    ios_base::openmode _Mode
);
void open(
    const wchar_t *_Filename,
    ios_base::openmode _Mode = ios_base::in | ios_base::out,
    int _Prot = (int)ios_base::_Openprot
);
void open(
    const wchar_t *_Filename,
    ios_base::openmode _Mode
);

接下来是参数的说明:

复制代码

_Filename
The name of the file to open.
打开文件名

_Mode
One of the enumerations in ios_base::openmode.
文件的打开方式(在ios_base::openmode中定义)

_Prot
The default file opening protection.
默认进行文件打开时的保护

复制代码
OK,我们再来看看ios_base::openmode中定义的打开方式:

复制代码

ios::in, to permit extraction from a stream.

打开文件进行读操作,即读取文件中的数据

ios::out, to permit insertion to a stream.

打开文件进行写操作,即输出数据到文件中

ios::app, to seek to the end of a stream before each insertion.

打开文件之后文件指针指向文件末尾,只能在文件末尾进行数据的写入

ios::ate, to seek to the end of a stream when its controlling object is first created.

打开文件之后文件指针指向文件末尾,但是可以在文件的任何地方进行数据的写入

ios::trunc, to delete contents of an existing file when its controlling object is created.

默认的文件打开方式,若文件已经存在,则清空文件的内容

ios::binary, to read a file as a binary stream, rather than as a text stream.

打开文件为二进制文件,否则为文本文件
复制代码
好了,open()函数的用法全部列举出来了。下面就针对ios_base::binary的二进制打开方式,我们在来谈一谈二进制文件的输出方式和文本文件的输出方式。

① 文本形式输出到文件,我们完全可以在open函数的mode选项中调用

ios::out|ios::app
好了,上面这句话说的就是将数据依次输出。注意,这里用的是依次,原因就是我们采用了app(append)模式,此表示在文件末尾继续写入文件,这就实现了数据的挨个写入 _。一个完整的程序例子如下:

#include <iostream>
#include<fstream>
using namespace std;
const int num=20;
struct people
{
    char name[num];
    double weight;
    int tall;
    int age;
    char sex;
};
int main()
{
    people pe={"李勇",78.5,181,25,'f'};
    ofstream fout("people.txt",ios::out|ios::app);
    fout<<pe.name<<" "<<pe.age<<" "<<pe.sex<<" "<<pe.tall<<" "<<pe.weight<<" "<<"\n";
    fout.close();
    ifstream fin("people.txt");
    char ch[255];
    fin.getline(ch,255-1,0);
    cout<<ch;
    fin.close();
    return 0;
}

输出如下:

在这里插入图片描述

  我们可以看到,people.txt文件中的内容和命令行中的一样。

  ② 二进制形式输出到文件 为了能够让其用二进制方式输出文件,我们只需要把上面程序的第16行和17行换做

1
2

ofstream fout("people.txt",ios::binary);
fout.write((char*)&pe,sizeof pe);

程序的第1行中的标志binary用于开启二进制模式,第2行调用了write函数。该函数有两个参数,第一个是要写入数据的首地址,在这里是结构体pe的地址,而第二个参数是要写入的字符数目,这里我们用sizeof来计算pe的字符数。具体程序如下:

#include <iostream>
#include<fstream>
using namespace std;
const int num=20;
struct people
{
    char name[num];
    double weight;
    int tall;
    int age;
    char sex;
};
int main()
{
    people pe={"李勇",78.5,181,25,'f'};
    ofstream fout("people.txt",ios::binary);
    fout.write((char*)&pe,sizeof pe);
    fout.close();
    people pe1={"张玲",65.4,165,62,'m'};
    ifstream fin("people.txt",ios::binary);
    fin.read((char*)&pe1,sizeof pe1);
    cout<<pe1.name<<" "<<pe1.age<<" "<<pe1.sex<<" "<<pe1.tall<<" "
        << pe1.weight <<" "<<"\n";
    fin.close();
    return 0;
}

我们再来看看这个东东的输出,我们可以看到,以txt文档打开文件时候,会产生乱码。这就是因为txt文件是以文本方式打开的,所以我们看到的都是乱码。如下图:

在这里插入图片描述
呼呼,以上就是我自认的自己不是很懂的C++关于如何操作文件的记录,到这里了~~全文完 _

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值