Question

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;
      }
  }
Was it helpful?

Solution

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, i.e. 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();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top