简体   繁体   English

std::wfstream 可以通过引用传递并且我能够正确使用 write() 和 read() 吗?

[英]Can std::wfstream passed by-reference AND will I be able to use write() and read() properly?

I use a self-made error/log-class for my project which is also be able to store/load itself into/from a file.我为我的项目使用自制的错误/日志类,它也能够将自身存储/加载到文件中/从文件中加载。 To make it easier for me when handling more objects I want to pass a file stream by-reference to this function which then uses the write/read member of the stream.为了让我在处理更多对象时更容易,我想通过引用此 function 来传递文件 stream,然后使用 ZF7B44CFAFD5C52223D5498196C8A2E7B 的写入/读取成员

Save with:保存:

in.open(L"XError.err",std::ios::in | std::ios::out | std::ios::binary | std::ios::trunc);

Load with:加载:

in.open(L"XError.err",std::ios::in | std::ios::out | std::ios::binary);

For saving:为了节省:

unsigned int HError::SaveToBufferW(std::wfstream& file)
{
    _ErrorSaveStruct ess = {0};
    ESS.IsUNICODE = true;
    ESS.ItemCount = 9999999;
    file.write((wchar_t*)&ess,sizeof(_ErrorSaveStruct) / sizeof(wchar_t));
    return 0;
}

For loading:加载:

int HError::LoadFromBufferW(std::wfstream& file)
{
    _ErrorSaveStruct ess = {0};
    file.read((wchar_t*)&ess,sizeof(_ErrorSaveStruct) / sizeof(wchar_t));
    return 0;
}

I checked the file and found out that nothing but whitespaces is written.我检查了文件,发现只写了空格 When I read/write a Unicode-string everything works and the string as well is readable in the file.当我读/写一个 Unicode 字符串时,一切正常,并且该字符串在文件中也是可读的。

Edit: here you go编辑:给你 go

 struct _ErrorSaveStruct
{
    unsigned int MsgSize;
    unsigned int TimeSize;
    unsigned int LastErrorSize;
    int ItemCount;
    int errState;
    bool InitMsg;
    bool IsUNICODE;
};

ok, used another answer to bypass my problem in some way:好的,使用另一个答案以某种方式绕过我的问题:

int main()
{   
HLib::_ErrorSaveStruct Ess = {0};  // just a namespace
Ess.IsUNICODE = true;
Ess.errState = 100;
Ess.ItemCount = 9999;

wchar_t* StringTest[1] = {L"WOORD"};    // same like (wchar_t[5])

FILE* iobuf = NULL;

// create new in binary read-write mode
if (_wfopen_s(&iobuf,L"WTest.err",L"wb+")==0) 
{
        //WRITING BLOCK
    if (fwrite(&Ess,sizeof(Ess),1,iobuf) != 1)  // sizeof_ErrorSaveStruct
        std::cout << "Fail (Reading struct)";

    if (fwrite(StringTest,sizeof(wchar_t) * 5,1,iobuf) != 1) // size = 5 
        std::cout << "Fail (Reading string)";

    // reset content
    SecureZeroMemory(&Ess,sizeof(Ess));
    SecureZeroMemory(StringTest,sizeof(wchar_t)*5);

    fseek(iobuf,0,0); // rewind
    fflush(iobuf);    // flush because you switch from write to read

     /// READING BLOCK
    if (fread(&Ess,sizeof(Ess),1,iobuf) != 1)   // sizeof_ErrorSaveStruct
        std::cout << "Fail (Reading struct)";

    if (fread(StringTest,sizeof(wchar_t) * 5,1,iobuf) != 1) // size = 5 
        std::cout << "Fail (Reading string)";
    fclose(iobuf);
}

return 0;

I really learned something about streams and file IO.我真的学到了一些关于流和文件 IO 的知识。 Thinking of other solutions, i also could have passed single parameters (int,bool) to wfstream instead of a whole struct.考虑其他解决方案,我也可以将单个参数(int,bool)传递给 wfstream 而不是整个结构。 It would have been possible as well to use std::fstream with binary flag set and pass a wchar_t string via write().也可以使用设置了二进制标志的 std::fstream并通过 write() 传递 wchar_t 字符串。 Anyway, this solution works well enough for me and I'm really happy about that.无论如何,这个解决方案对我来说已经足够好了,我对此感到非常高兴。

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

相关问题 如何从wfstream读取二进制数据? - How can I read binary data from wfstream? 为什么我不能在 lambda 中捕获这个引用('&amp;this')? - Why can't I capture this by-reference ('&this') in lambda? 不成功的调用会改变按引用传递的结果吗? - Will unsuccessful call alter by-reference passed result? 提供给std :: unique_ptr的简单自定义删除器lambda:为什么使用by-reference capture default([&])over no capture([])? - Simple custom deleter lambda supplied to std::unique_ptr: why use by-reference capture default ([&]) over no-capture ([])? 如何让 C++ wfstream 使用 Unicode 文件路径? - How can I get C++ wfstream to work with Unicode filepath? 为什么我不能在std :: future参数中使用reference - Why can't I use reference in std::future parameters 我可以使用std :: vector :: operator []返回的引用多长时间? - how long can I use reference returned by std::vector::operator[]? 为什么我不能将 std::iterator&lt;&gt;::reference 与模板一起使用? - Why can't I use std::iterator<>::reference with templates? 如何使用 std::reference_wrapper 作为 class 成员? - How can I use std::reference_wrapper as a class member? 如何使用std :: lock_guard锁定对std :: map的读写访问权限? - How do I use std::lock_guard to lock read and write access to a std::map?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM