c++文件操作:2深入

继续上面<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.
打开文件为二进制文件,否则为文本文件

ios_base::openmode中定义的打开方式

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行换做

         ofstream fout("people.txt",ios::bianry);

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

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

#include <iostream>
#include <fstream>
using namespace std;
const int num=20;//人员编号的最大值是20

struct people//人员结构体
{
    char name[num];//人员编号
    double weight;//人的体重
    int tall;//人的身高
    int age;//人的年龄
    char sex;//人的性别
};
int main()
{
    people pe={"李勇",78.5,181,25,'f'};//定义结构体对象pe
    ofstream fout("people.txt",ios::binary);//以二进制方式打开文件“people.txt”
    fout.write((char*)&pe,sizeof pe);//将结构体pe的数据写入文件
    fout.close();//关闭文件流

    people pe1={"张琳",65.4,165,62,'m'};//定义结构体对象pe1
    //下面,我们来试一下把pe1中的内容用people.txt文件中的内容替换掉
    ifstream fin("people.txt",ios::binary);//以二进制形式打开文件,读取文件内容
    fin.read((char*)&pe1,sizeof pe1);//把文件里的信息写入变量pe1
    cout<<pe1.name<<" "<<pe1.age<<" "<<pe1.sex<<" "<<pe1.tall<<" "
        << pe1.weight <<" "<<"\n";//输出变量pe1的内容
    fin.close();//关闭文件流
    return 0;
}

运行效果:

看到屏幕输出是正常的,而txt文本里的输出却出现乱码。这就是因为people.txt文件是以文本方式打开的,而不是以二进制形式打开的,所以我们看到的都是乱码。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值