简体   繁体   中英

c++ _bstr_t instead of use string

I have some code to read file,but it is used std::string, i need to use _bstr_t the code follow can works fine. how to change the type?

   std::ifstream inFile("QdatPassWordconfig.config");
  std::string sPassWord; //(here, i need to use _bstr_t)
  std::string sTemp;
  if (inFile.is_open())
  {
      if(std::getline(inFile,sTemp)) 
      {
            cout<<sTemp<<endl;
            sPassWord=sTemp;
      }
  }

If you read the _bstr_t documentation , you will see that its assignment operator takes a normal const char * .

So it's probably just to assign to it:

_bstr_t sPassword;

// ...

sPassword = sTemp.c_str();

If you have trouble using normal narrow-character strings, you should convert all your code relating to this to use wide-character string, ie the classes with the w prefix:

std::wifstream inFile("QdatPassWordconfig.config");
_bstr_t sPassword;
std::wstring sTemp;

if (inFile.is_open())
{
    if(std::getline(inFile, sTemp)) 
    {
        std::wcout << sTemp << endl;

        sPassword = sTemp.c_str();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM