为什么wifstream.read函数没有将数据读取到变量?

时间:2018-07-07 15:32:11

标签: c++ file c++11 fstream

我的结构是:

struct purchase_order_details{
    wstring number;
    wstring date;
    wstring vender_code;
    void Write(wofstream&);
    void Read(wifstream&);
    size_t totalSizeInFile;
};

以上功能实现如下:

void purchase_order_details::Write(wofstream& wofs)
{
    size_t totalSize = 0;

    size_t s1 = this->date.size(); totalSize += s1;
    wofs.write((wchar_t*)&s1, sizeof(s1));
    wofs.write(  this->date.c_str() , s1 );

    s1 = this->number.size(); totalSize += s1;
    wofs.write((wchar_t*)&s1, sizeof(s1));
    wofs.write(  this->number.c_str() , s1 );

    s1 = this->vender_code.size(); totalSize += s1;
    wofs.write((wchar_t*)&s1, sizeof(s1));
    wofs.write(  this->vender_code.c_str() , s1 );

    totalSizeInFile = totalSize;
}

void purchase_order_details::Read(wifstream& wifs)
{
size_t sz=0;

wifs.read((wchar_t*)&sz, sizeof(sz));
wchar_t * date = new wchar_t[sz];
wifs.read(date, sz);
this->date = date;

wifs.read((wchar_t*)&sz, sizeof(sz));
wchar_t * number = new wchar_t[sz];
wifs.read(number, sz);
this->number = number;

wifs.read((wchar_t*)&sz, sizeof(sz));
wchar_t * vcode = new wchar_t[sz];
wifs.read(vcode, sz);
this->vender_code = vcode;

delete []date;
delete []number;
delete []vcode;
}

因为我的结构包含一个wstring,所以我必须实现一种从文件读取和写入数据的不同方法。

问题是在Read函数wifs.read((wchar_t*)&sz, sizeof(sz));中,另一个函数也没有从文件中读取值到变量。

传递给两个函数的文件均为二进制。

为什么不将值读入变量?解决办法是什么?

在此先感谢并提供任何建议。

1 个答案:

答案 0 :(得分:1)

与允许别名std::ofstream的{​​{1}}不同,您不能使用(char*)&integer通过别名std::wofstream来写入二进制数据。您可以通过以下错误代码重现该错误:

(wchar_t*)&integer

std::wofstream fout(L"unicode.txt", ios::binary); int integer = 0x12345678; fout.write((wchar_t*)&integer, sizeof(integer)); if(!fout.good()) cout << "problem\n"; //<- write fails, file size should be zero 的目的是将文件中的字节转换为2个字节的宽字符。如果要处理二进制数据,则只能使用单个字节,因此请使用std::wstream

Visual Studio为std::fstream提供了一个特殊的构造函数和open方法,该方法允许使用Unicode文件名,这将为处理具有Unicode名称的文件提供必要的Unicode兼容性。

fstream

并确保以二进制模式打开文件:

void Write(ofstream& ofs)
{
    size_t sz = date.size();
    ofs.write((char*)&sz, sizeof(sz)); ofs.write((char*)date.c_str(), sz);

    sz = number.size(); 
    ofs.write((char*)&sz, sizeof(sz)); ofs.write((char*)number.c_str(), sz);

    sz = vender_code.size();
    ofs.write((char*)&sz, sizeof(sz)); ofs.write((char*)vender_code.c_str(), sz);
}

void Read(std::ifstream& ifs)
{
    size_t sz = 0;

    ifs.read((char*)&sz, sizeof(sz)); date.resize(sz + 1, 0);
    ifs.read((char*)&date[0], sz);

    ifs.read((char*)&sz, sizeof(sz)); number.resize(sz + 1, 0);
    ifs.read((char*)&number[0], sz);

    ifs.read((char*)&sz, sizeof(sz)); vender_code.resize(sz + 1, 0);
    ifs.read((char*)&vender_code[0], sz);
}

或者,将Unicode从UTF16转换为UTF8,然后使用标准库的输入/输出purchase_order_details info; info.date = L"date"; info.number = L"number"; info.vender_code = L"vendor_code"; ofstream fout(L"unicode.txt", std::ios::binary); info.Write(fout); fout.close(); ifstream fin(L"unicode.txt", std::ios::binary); info.Read(fin); / >>运算符以纯文本形式编写整数。输出文件也将与POSIX兼容。