Search code examples
stringvisual-c++file-iobstr

Visual C++ Write BSTR to File


I am a newbie to C++. I am trying to write a BSTR to a CSV file, but the data when I print with wcout is not matching with data what's present in file.

BSTR tempString;
ofstream outputFile;
outputFile.open("C:\\data.csv",ios::out);
tempString = getData();
outputFile.write(tempString);
outputFile.close();

BSTR getData()
{
   BSTR KBIDValue;
   IStringCollection *KBID;
   KBID->get_Item(0,&KBIDValue);
   return KBIDValue;
}
  1. If tempString = L"TestData" -> I am able to see the same value in file.
  2. If tempString = getData(); where the function returns a BSTR, I am not able to see same value in file.

Could some please clarify this? Also please explain what exactly is the method to write BSTR to file?

EDIT: Added code


Solution

  • I have added the following lines of code in my program and giving the results correctly.

    ofstream outputFile;
    outputFile.open(filePath,ios::out);
    outputFile << W2A(CString(tempString));