繁体   English   中英

使用流将类对象写入文件

[英]Writing class object to file using streams

我有这段代码可以将类对象序列化/反序列化到文件,并且似乎可以正常工作。 但是,我有两个问题。

  1. 如果我想在类中使用一个wstring和一个string成员变量,而不是两个wstring (现在已经拥有)怎么办? (我认为在这种情况下我的代码行不通?)。
  2. 最后,下面,主要是当我初始化s2.product_name_= L"megatex"; 如果我不是用megatex而是用俄语写一些东西(例如s2.product_name_ = L“логин”),那么该代码将无法正常工作。

有什么事吗 谢谢。

这是代码:

// ConsoleApplication3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>      // std::ifstream
using namespace std;

// product
struct Product
{
    double price_;
    double product_index_;
    wstring product_name_;
    wstring other_data_;

    friend std::wostream& operator<<(std::wostream& os, const Product& p)
    {
         return os << p.price_ << endl
                  << p.product_index_ << endl
                  << p.product_name_ << endl  
                  << p.other_data_ << endl;
    }

    friend wistream& operator>>(std::wistream& is, Product& p)
    {
        is >> p.price_ >> p.product_index_;
        is.ignore(std::numeric_limits<streamsize>::max(), '\n');

        getline(is,p.product_name_);
        getline(is,p.other_data_);

        return is;
    }
};

 int _tmain(int argc, _TCHAR* argv[])
{
    Product s1,s2;

    s1.price_ = 100;
    s1.product_index_ = 0;
    s1.product_name_= L"flex";
    s1.other_data_ = L"dat001";

    s2.price_ = 300;
    s2.product_index_ = 2;
    s2.product_name_= L"megatex";
    s2.other_data_ = L"dat003";

    // write
    wofstream binary_file("c:\\test.dat",ios::out|ios::binary|ios::app);
    binary_file << s1 << s2;
    binary_file.close();

    // read
    wifstream binary_file2("c:\\test.dat");

    Product p;
    while (binary_file2 >> p)
    {
        if(2 == p.product_index_){
            cout<<p.price_<<endl;
            cout<<p.product_index_<<endl;
            wcout<<p.product_name_<<endl;
            wcout<<p.other_data_<<endl;
        }
    }

    if (!binary_file2.eof())
         std::cerr << "error during parsing of input file\n";
    else
        std::cerr << "Ok \n";

    return 0;
}

如果要在我的班级中使用两个wstring(现在是我现在)要一个wstring和一个string成员变量怎么办? (我认为在这种情况下我的代码行不通?)。

对于任何basic_ostreamostreamwostream ),都为char *定义了一个插入器,因此您可以将c_str()成员函数调用的结果用于string成员。 例如,如果string成员是other_data_

 return os << p.price_ << endl
           << p.product_index_ << endl
           << p.product_name_ << endl  
           << p.other_data_.c_str() << endl;

提取器的情况更为复杂,因为您必须将其读取为wstring并将其转换为string 最简单的方法是将其读为wstring ,然后缩小每个字符的范围:

wstring temp;
getline(is, temp);
p.other_data_ = string(temp.begin(), temp.end());

在此示例中,我没有使用语言环境,只是将字节序列(8位)转换为单词序列(16位)用于输出,而将相反的(截断值)用于输入。 如果您使用的是ASCII字符或单字节字符,并且您不需要特定的格式(如Unicode)进行输出,则可以。

否则,您将需要使用locale处理。 语言环境提供文化上下文信息来解释字符串(请记住,这只是一个字节序列,而不是字母或符号意义上的字符;字节与符号所代表的关系之间的映射由locale定义)。 locale不是一个非常易于使用的概念(人类文化也不是)。 正如您所建议的那样,最好先对其工作方式进行一些调查。

无论如何,这个想法是:

  1. 标识字符串中使用的字符集和文件中使用的字符集(Unicode或utf-16)。
  2. 使用localestring s从原始字符集转换为Unicode以进行输出。
  3. 使用locale将从文件读取的wstring (以Unicode格式)转换为string

最后,下面,主要是当我初始化s2.product_name_ = L“ megatex”时; 如果我不是用megatex而是用俄语写一些东西(例如s2.product_name_= L"логин" ),那么该代码将无法正常工作。

当使用L""定义wchar_t数组时,您实际上并没有指定字符串为Unicode,只是该数组是chars,而不是wchar_t。 我想预期的工作是s2.product_name_以Unicode格式存储名称,但是编译器将采用该字符串中的每个char (与不使用L ),并转换为wchar_t仅用零填充最高有效字节。 直到C ++ 11为止,C ++标准中对Unicode的支持都还不错(并且仍然不太真正支持)。 它仅适用于ASCII字符,因为它们在Unicode(或UTF-8)中具有相同的编码。

要在静态字符串中使用Unicode字符,可以使用转义字符: \\uXXXX 我知道,对每个非英语角色这样做都不是一件容易的事。 您可以在网络上的多个站点中找到Unicode字符列表。 例如,在Wikipedia中: http : //en.wikipedia.org/wiki/List_of_Unicode_characters

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM