1

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;
      }
  }
congusbongus
  • 13,359
  • 7
  • 71
  • 99
jack.li
  • 973
  • 1
  • 9
  • 20

1 Answers1

3

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();
    }
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621