在二进制文件 c++ 中读取和写入字符串

Reading and write strings in binary files c++

本文关键字:字符串 读取 二进制文件 c++      更新时间:2023-10-16

我正在尝试开发一个小型Windows应用程序,以提高我在MFC框架之外的C++技能,并帮助我学习外语。
我想制作一个小的、个人的、易于port_and_use的字典,虽然我在开发 GUI 方面没有问题,但我在保存和恢复数据方面遇到了真正的痛苦。

我的想法是写下结构如下的二进制文件:

int (representing the number of words)
int (representing the string length + )
sequence of characters zero-terminated.
现在,我正在学习俄语,我的主要语言是意大利语,所以我不能使用普通的旧 std::string 来写下单词,此外,谢谢Microsoft,我正在使用 VS2010 与它附带的所有商品和坏处。我正在向您展示我的例程,以写下int和wstring:
//Writing int
void CDizionario::ScriviInt( int nInt, wofstream& file ) const
{
    file.write( reinterpret_cast < const wchar_t * > ( &nInt ), sizeof( nInt ) );
    file.flush();
}
// Writing string
void CDizionario::ScriviWString( int nLStringa, const wstring* pStrStringa, wofstream& file ) const
{
    wchar_t cTerminatore;
    string strStringa;
    file.write( pStrStringa->c_str(), nLStringa );
    file.flush();
    cTerminatore = L'';
    file.write( &cTerminatore, sizeof( wchar_t ) );
    file.flush();
}
// Reading int
void CDizionario::LeggiInt( int *pInt, wifstream& file )
{
    file.read( reinterpret_cast < wchar_t * >( pInt ), sizeof( int ) );
}
// Reading wstring
void CDizionario::LeggiWString( int nLStringa, wstring& strStringa, wifstream& file )
{
    wchar_t *pBuf;
    streamsize byteDaLeggere;
    byteDaLeggere = nLStringa;
    pBuf = new wchar_t[(unsigned int)( byteDaLeggere * sizeof( wchar_t ) )];
    file.read( pBuf, byteDaLeggere * sizeof( wchar_t ) );
    strStringa.append( pBuf );
    delete [] pBuf;
}
// Constructor
CDizionario::CDizionario( void )
{
    m_pLoc = new locale( locale::classic(), new codecvt_utf8_utf16 );
}
// Somewhere in my code before calling LeggiInt/ScriviInt/LeggiWString/ScriviWString:
// ...
file.imbue( *m_pLoc );

好吧,我的第一个测试是:ciao - привет,结果:

01 00 ee bc 90 22 05 00 ee bc 90 22 63 69 61 6f
00 ec b3 8c 07 00 ee bc 90 22 d0 bf d1 80 d0 b8
d0 b2 d0 b5 d1 82 00 ec b3 8c
数字被正确读取,当我写下字符串时出现问题:我希望 ciao (63 69 61 6f 00 ec b3 8c( 是用 10 个字节(wchar_t 大小(而不是 5 写的,就像俄语翻译一样( d0 bf d1 80 d0 b8 d0 b2 d0 b5 d1 82 00 ec b3 8c(。
显然我错过了一些东西,但我无法弄清楚它是什么。你们能帮帮我吗?另外,如果你知道解决问题的更好方法,我是开放的。

编辑:解决方案

遵循@JamesKanze提出的两种方法中的第一种方法,我决定牺牲一些可移植性,让系统做功课:

void CDizionario::LeggiInt( int *pInt, ifstream& file )
{
    file.read( reinterpret_cast( pInt ), sizeof( int ) );
}

void CDizionario::LeggiWString( int nLStringa, wstring& strStringa, ifstream& file ) { char *pBuf; streamsize byteDaLeggere; wstring_convert> converter; byteDaLeggere = nLStringa; pBuf = new char[byteDaLeggere]; file.read( pBuf, byteDaLeggere ); strStringa = converter.from_bytes( pBuf ); delete [] pBuf; }

void CDizionario::ScriviInt( int nInt, ofstream& file ) const { file.write( reinterpret_cast( &nInt ), sizeof( nInt ) ); file.flush(); } void CDizionario::ScriviWString( const wstring* pStrStringa, ofstream& file ) const { char cTerminatore; string strStringa; wstring_convert> converter; strStringa = converter.to_bytes( pStrStringa->c_str() ); ScriviInt( strStringa.length() + 1, file ); file.write( strStringa.c_str(), strStringa.length() ); file.flush(); cTerminatore = ''; file.write( &cTerminatore, sizeof( char ) ); file.flush(); }

您尚未充分指定二进制文件的格式。你如何表示int(多少字节,大端或小端序(,也没有编码和格式字符。 经典的网络表示形式将是大端四字节(无符号(整数和 UTF-8。 因为这是你为自己做的事情,你可以(和可能应该(简化,使用小端序作为整数,以及UTF-16LE;这些格式对应于下面的内部格式窗户。 (请注意,这样的代码是不可移植的,甚至不是到苹果或Linux在同一架构上,并且有数据在新系统上变得不可读的可能性很小。这基本上是你似乎在尝试的,但是...

您正在尝试编写原始二进制文件。 唯一的标准方法这将是使用 std::ofstream (和 std::ifstreamread(,文件以二进制模式打开充满 "C"区域设置。 对于其他任何事情,将会(或可能(有一些std::filebuf中的代码转换和映射排序。鉴于此(以及这种写入数据的方式不是可移植到任何其他系统(,您可能只想使用系统级功能:CreateFile打开、WriteFileReadFile写入和读取,CloseHandle关闭。 (见http://msdn.microsoft.com/en-us/library/windows/desktop/aa364232%28v=vs.85%29.aspx(。

另一方面,如果你想便携,我会推荐对数据使用标准网络格式。 将其格式化为一个缓冲区(std::vector<char> (,然后写出来;在另一个结束,读入缓冲区,然后解析它。 读写整数(实际上是无符号整数(的例程可能是像这样:

void
writeUnsignedInt( std::vector<char>& buffer, unsigned int i )
{
    buffer.push_back( (i >> 24) & oxFF );
    buffer.push_back( (i >> 16) & oxFF );
    buffer.push_back( (i >>  8) & oxFF );
    buffer.push_back( (i      ) & oxFF );
}
unsigned int
readUnsignedInt( 
    std::vector<char>::const_iterator& current,
    std::vector<char>::const_iterator end )
{
    unsigned int retval = 0;
    int shift = 32;
    while ( shift != 0 && current != end ) {
        shift -= 8;
        retval |= static_cast<unsigned char>( *current ) << shift;
        ++ current;
    }
    if ( shift != 0 ) {
        throw std::runtime_error( "Unexpected end of file" );
    }
    return retval;
}

对于字符,您必须将 std::wstring 转换为std::字符串,采用 UTF-8 格式,使用众多转换例程之一在网络上可用。 (问题是编码 std::wstring,甚至不是wchar_t的大小,也不是标准化。 在我熟悉的系统中,Windows和AIX使用 UTF-16,大多数其他 UTF-32;在这两种情况下,字节订单取决于平台。 这使得可移植代码有点更难。

在全球范围内,我发现直接在UTF-8,使用 char . 这不适用于Windows。但是,界面。

最后,如果您输出,则不需要尾随''长度。

@IssamTP, привет

正如 @James Kanze 所提到的,使用外来非拉丁语言不可避免地会促使您采用每字节格式约定和区域设置。因此,不重新发明轮子并使用XML等现有技术可能是值得的(因此该技术将提供细微差别并正确编码/解码非拉丁字符(。