【问题标题】:Opening files with wstring path in MinGW在 MinGW 中使用 wstring 路径打开文件
【发布时间】:2020-09-30 18:02:18
【问题描述】:

我正在使用library for parsing Standard MIDI Files,它允许您以两种方式读取文件:

    bool MidiFile::read(const std::string& filename) {/* --- */}
    //and
    bool MidiFile::read(std::istream& input) {/* --- */}

要在 MSVC 中使用 wstring 路径打开 MIDI 文件,我可以这样做:

    MidiFile *mf = new MidiFile();
    fstream input;
    input.open(L"Файл.mid", ios::binary | ios::in);
    mf->read(input);
    input.close();

但似乎 GCC 没有像 MSVC 中那样对 wstring 类型的 fstream::open 方法进行重载。如何在 MinGW 中打开带有 wsting 路径的文件并使用它传递给 Midifile 库?或者有没有办法以 C 风格或其他库打开文件,并以某种方式将文件中的数据转换为 std::istream

【问题讨论】:

    标签: c++ boost mingw fstream


    【解决方案1】:

    这是一个 C++17 特性。确保您的编译器足够新,并且至少使用 -std=c++17 进行构建。

    请注意,它只会在std::filesystem::path::value_typewchar_t 时编译,换句话说 - 仅在 Windows 上。要使代码可移植,请先将路径转换为std::filesystem::path

    【讨论】:

      【解决方案2】:

      我找到了一个可接受的解决方案,尽管速度较慢:

      #include <string>
      #include <iostream>
      #include <sstream>
      #include "craigsapp-midifile/MidiFile.h"
      
      int main()
      {
          FILE* fp;
          _wfopen_s(&fp, L"МИДИ.mid", L"rb");
          fseek(fp, 0, SEEK_END);
          long fsize = ftell(fp);
          fseek(fp, 0, SEEK_SET);
      
          std::string str(fsize, '*');
      
          for (int i = 0; i < str.size(); ++i) 
              str[i] = (char)getc(fp);
          fclose(fp);
      
          std::stringstream binarydata;
          binarydata << str;
      
          smf::MidiFile mf;
          if (mf.read(binarydata)) 
              std::cout << "midi read success!" << std::endl;
      
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 2013-07-14
        • 1970-01-01
        • 1970-01-01
        • 2021-12-31
        • 1970-01-01
        • 1970-01-01
        • 2020-09-15
        • 2019-04-11
        • 1970-01-01
        相关资源
        最近更新 更多