<tfoot id='RGoTb'></tfoot>
      • <bdo id='RGoTb'></bdo><ul id='RGoTb'></ul>

        <small id='RGoTb'></small><noframes id='RGoTb'>

      1. <i id='RGoTb'><tr id='RGoTb'><dt id='RGoTb'><q id='RGoTb'><span id='RGoTb'><b id='RGoTb'><form id='RGoTb'><ins id='RGoTb'></ins><ul id='RGoTb'></ul><sub id='RGoTb'></sub></form><legend id='RGoTb'></legend><bdo id='RGoTb'><pre id='RGoTb'><center id='RGoTb'></center></pre></bdo></b><th id='RGoTb'></th></span></q></dt></tr></i><div id='RGoTb'><tfoot id='RGoTb'></tfoot><dl id='RGoTb'><fieldset id='RGoTb'></fieldset></dl></div>
        <legend id='RGoTb'><style id='RGoTb'><dir id='RGoTb'><q id='RGoTb'></q></dir></style></legend>

        我可以使用 CreateFile,但将句柄强制转换为 std:

        时间:2023-08-02
      2. <i id='W2MMe'><tr id='W2MMe'><dt id='W2MMe'><q id='W2MMe'><span id='W2MMe'><b id='W2MMe'><form id='W2MMe'><ins id='W2MMe'></ins><ul id='W2MMe'></ul><sub id='W2MMe'></sub></form><legend id='W2MMe'></legend><bdo id='W2MMe'><pre id='W2MMe'><center id='W2MMe'></center></pre></bdo></b><th id='W2MMe'></th></span></q></dt></tr></i><div id='W2MMe'><tfoot id='W2MMe'></tfoot><dl id='W2MMe'><fieldset id='W2MMe'></fieldset></dl></div>
            <bdo id='W2MMe'></bdo><ul id='W2MMe'></ul>
              <tbody id='W2MMe'></tbody>

          • <legend id='W2MMe'><style id='W2MMe'><dir id='W2MMe'><q id='W2MMe'></q></dir></style></legend>
                1. <small id='W2MMe'></small><noframes id='W2MMe'>

                  <tfoot id='W2MMe'></tfoot>

                  本文介绍了我可以使用 CreateFile,但将句柄强制转换为 std::ofstream 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  有什么方法可以利用 Win32 API 中的文件创建标志,例如 FILE_FLAG_DELETE_ON_CLOSEFILE_FLAG_WRITE_THROUGH,如此处所述http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx ,然后将该句柄强制转换为 std::ofstream ?

                  Is there any way to take advantage of the file creation flags in the Win32 API such as FILE_FLAG_DELETE_ON_CLOSE or FILE_FLAG_WRITE_THROUGH as described here http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx , but then force that handle into a std::ofstream?

                  ofstream 的接口显然是平台无关的;我想在幕后"中强制执行一些平台相关的设置.

                  The interface to ofstream is obviously platform independent; I'd like to force some platform dependent settings in 'under the hood' as it were.

                  推荐答案

                  可以将 C++ std::ofstream 附加到 Windows 文件句柄.以下代码适用于 VS2008:

                  It is possible to attach a C++ std::ofstream to a Windows file handle. The following code works in VS2008:

                  HANDLE file_handle = CreateFile(
                      file_name, GENERIC_WRITE,
                      0, NULL, CREATE_ALWAYS,
                      FILE_ATTRIBUTE_NORMAL, NULL);
                  
                  if (file_handle != INVALID_HANDLE_VALUE) {
                      int file_descriptor = _open_osfhandle((intptr_t)file_handle, 0);
                  
                      if (file_descriptor != -1) {
                          FILE* file = _fdopen(file_descriptor, "w");
                  
                          if (file != NULL) {
                              std::ofstream stream(file);
                  
                              stream << "Hello World
                  ";
                  
                              // Closes stream, file, file_descriptor, and file_handle.
                              stream.close();
                  
                              file = NULL;
                              file_descriptor = -1;
                              file_handle = INVALID_HANDLE_VALUE;
                          }
                  }
                  

                  这适用于 FILE_FLAG_DELETE_ON_CLOSE,但 FILE_FLAG_WRITE_THROUGH 可能没有预期的效果,因为数据将被 std::ofstream 对象缓冲,而不是直接写入磁盘.但是,当调用 stream.close() 时,缓冲区中的任何数据都将刷新到操作系统.

                  This works with FILE_FLAG_DELETE_ON_CLOSE, but FILE_FLAG_WRITE_THROUGH may not have the desired effect, as data will be buffered by the std::ofstream object, and not be written directly to disk. Any data in the buffer will be flushed to the OS when stream.close() is called, however.

                  这篇关于我可以使用 CreateFile,但将句柄强制转换为 std::ofstream 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:SendInput() 不等于在 C++ 键盘上手动按键? 下一篇:WinAPI Sleep() 函数调用休眠的时间比预期的要长

                  相关文章

                  最新文章

                2. <i id='Y9M7E'><tr id='Y9M7E'><dt id='Y9M7E'><q id='Y9M7E'><span id='Y9M7E'><b id='Y9M7E'><form id='Y9M7E'><ins id='Y9M7E'></ins><ul id='Y9M7E'></ul><sub id='Y9M7E'></sub></form><legend id='Y9M7E'></legend><bdo id='Y9M7E'><pre id='Y9M7E'><center id='Y9M7E'></center></pre></bdo></b><th id='Y9M7E'></th></span></q></dt></tr></i><div id='Y9M7E'><tfoot id='Y9M7E'></tfoot><dl id='Y9M7E'><fieldset id='Y9M7E'></fieldset></dl></div>

                    <legend id='Y9M7E'><style id='Y9M7E'><dir id='Y9M7E'><q id='Y9M7E'></q></dir></style></legend>
                  1. <tfoot id='Y9M7E'></tfoot>
                      <bdo id='Y9M7E'></bdo><ul id='Y9M7E'></ul>

                    <small id='Y9M7E'></small><noframes id='Y9M7E'>