Introduction to Programming with c++ 13-7 BinaryIO

这一目主要是对BinaryIO的一些基本概念坐下总结。代码虽然不难写,不过这些基本概念倒是值得好好理解理解。

基本概念

Files can be classified into text and binary.

text file and binary file

A file that can be processed(read, created or modified) using a text editor is called a text file.All the files are called binary files. For example, the C++ source code are stored in text files and can be read by a text editor, but the c++ executable files are stored in binary files and are read by the operating sysytems.
(PS:这一段并没有从本质上揭示二者区别,只是给了一个形象的描述。)

more about text file and binary file

You can envision a text file as consisting of a sequence of characters and a bainary file as consisting of a sequence of bits.
(PS:这一段进一步说明了,一个是字节序列,一个是比特序列。)

文章此时举了一个199的例子,如果是存在text file, 那么199是 ‘1’, ‘9’, ‘9’这三个字符。如果是binary file, 就是199在计算机内存中表示。

内存表示如下:用表格代替下,每个表格看做内存中的一个字节。字符用ascii编码的形式存储。那么’1’(49),’9’(57)

  • 199 (text file)
Memory
00110001
00111011
00111011
  • 199(binary file)
Memory
11000111

从内存表示来看,显然binary file具有更高的存储效率。

Note

Computer do not differentiate binary files and text files. All files are stored in binary format, and all files are essentially binary files.Text I/O is built upon binary I/O to provide a level of abstraction for character encoding and decoding.
(PS:这一部分说的非常的精髓,二者本质上都是binary file,只不过text file 在此基础上做了一层抽象,对字符做了编码和解码,从而使得text file变成有结构数据,就和网络通信过程中下层对上层的封装一样。)


回到本章开始的地方,再总结一些基本概念。

stream

C++ uses the term stream to describe a flow of data.If it flows to your programme, the stream is called input stream. If it flows out from your programme, it it called an output stream.
C++ uses objects to read/write a stream of data.For convenience, an input object is called input stream which processing and manipulating input streams , output object is called output stream which processing and manipulating output streams .


代码

BinaryIO不常用,接口如下:

streamObject.write( char* address, int size );
streamObject.read( char* address, int size );
//这个感觉和c的也比较像,就是从address这个地址,读取size个字节。
//对于第二个函数,size是期望读取的字节数,但是实际读取的用函数gcount获得。

下面这部分代码针对string类型的,所以参数传递的时候不牵扯到转换。

#include <iostream>
#include <fstream>
#include <string>

int write_binary_file( const std::string& word );
std::string read_binary_file();

int main()
{
    std::string s = "China";
    write_binary_file( s );

    std::string t = read_binary_file();
    std::cout << t << std::endl;

    return 0;
}

int write_binary_file( const std::string& word )
{
    std::ofstream fout;
    fout.open( "input.dat", std::ios::binary );
    if( !fout.is_open() )
    {
        std::cerr << "Can not open the file!" << std::endl;
        return -1;
    }

    fout.write( word.c_str(), word.size() );

    fout.close();
    return 0;
}
std::string read_binary_file()
{
    static const int maxn = 32;
    char tmp[maxn];

    std::ifstream fin;
    fin.open( "input.dat", std::ios::binary );
    if( !fin.is_open() )
    {
        std::cerr << "Can not open the file!" << std::endl;
        return NULL;
    }

    fin.read( tmp, maxn - 1 ); // the last character for '\0'
    tmp[fin.gcount()] = '\0';

    fin.close();

    std::string ret( tmp, tmp + fin.gcount() );
    return ret;
}

下面补充针对非string类型的代码,以int类型为例。
reinterpret_cast这个关键字之前没有接触过,感觉用在地址强转上。这个后序再总结吧。

int write_binary_file1( int val )
{
    std::ofstream fout;
    fout.open( "input.dat", std::ios::binary );
    if( !fout.is_open() )
    {
        std::cerr << "Can not open the file!" << std::endl;
        return -1;
    }

    fout.write( reinterpret_cast<char*>(&val), sizeof(int) );

    fout.close();
    return 0;
}
int read_binary_file1()
{
    int ret = 0;

    std::ifstream fin;
    fin.open( "input.dat", std::ios::binary );
    if( !fin.is_open() )
    {
        std::cerr << "Can not open the file!" << std::endl;
        return -1;
    }

    fin.read( reinterpret_cast<char*>(&ret) , sizeof(int) ); 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值