提升openfilename引起的mapped_file_source异常

时间:2014-11-18 21:15:25

标签: c++ windows winapi boost

    #include<iostream>
    #include<boost/iostreams/device/mapped_file.hpp>
    #include<boost/iostreams/stream.hpp>
    #include<windows.h>

   int main() 
    {
      /*/----- first part starts here
     OPENFILENAMEA ofn;
     char szFileName[MAX_PATH];
     ZeroMemory(&ofn, sizeof(ofn));
     szFileName[0] = 0;
     ofn.lStructSize = sizeof(ofn);
     ofn.hwndOwner = 0;
     ofn.lpstrTitle="Open file";
     ofn.lpstrFilter ="All Files\0*.*\0";
     ofn.nMaxFile = MAX_PATH;
     ofn.lpstrFile=szFileName;
     ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST ;
     if(!GetOpenFileNameA(&ofn))return 0;
     //-------- first part ends here---*/

  double bytesRead=0;
  boost::iostreams::mapped_file_source ifile;
  ifile.open("D:\\aaa.mp4");//ofn.lpstrFile should be here as argument
  if(ifile.is_open()){   
    boost::iostreams::stream<boost::iostreams::mapped_file_source> input(ifile);
    const int chunksize      = 4096;
    const int fsize          = ifile.size();
    const int rest           = ifile.size() % chunksize;
    const int numberOfchunks = ifile.size() / chunksize;    

    __int64 bytes[chunksize/sizeof(__int64)];
    for (int x=0; x<numberOfchunks; x++){
        input.read((char*)bytes, chunksize);
        bytesRead += chunksize;
    }
    if (rest > 0){
       input.read((char*)bytes, rest);
       bytesRead += rest;
    }
    input.close();
    std::cout << std::endl << bytesRead / (1024*1024) << " MB read" << std::endl;
 }
system("pause");
return 0;
 }
如果&#34; 第一部分&#34;它会让我发疯。未被注释掉,甚至没有使用&#34; ofn.lpstrfile&#34;打开文件会引发异常:

   boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector

但是如果我注释掉&#34; 第一部分&#34;我没有使用的代码,但文件读取没有问题!!请问有什么想法吗?

1 个答案:

答案 0 :(得分:0)

试试这个:

 GetOpenFileName()     changes the current directory
 ifile.open("D:\\aaa.mp4");' will not find the file  :)