我有一个BSTR,里面有很多的混血儿。
BSTR theFile = NULL;
int res = comSmartspeak -> readTheFile(fileName, &theFile);我想读第一行,但我不知道怎么做。
这是我想出的pseudoCode:
string firstLine = "";
for (int i = 0; i < SysStringLen(theFile) ; i++)
{
if (theFile[i] == '\n')
{
break;
}else
{
firstLine += theFile[i] ;
}
}我是VC++的新手。
发布于 2015-10-09 15:36:13
//another option
BSTR comStr = ::SysAllocString(L"First line of text\nSecond line of text\nThird line of text\nFourth line of text...");
std::wstring ws(comStr, SysStringLen(comStr));
std::wstring::size_type pos = ws.find_first_of('\n');
if (pos != std::wstring::npos)
{
std::wcout << ws.substr(0, pos);
}发布于 2015-10-09 14:40:30
您可以这样做,前提是您可以自由地使用std::wstring,这样您就不必事先计算第一行的长度:
#include <Windows.h>
#include <iostream>
#include <string>
int main()
{
BSTR a_bstr = ::SysAllocString(L"First line of text\nSecond line of text\nThird line of text\nFourth line of text...");
std::wstring wide_str;
unsigned int len = ::SysStringLen(a_bstr);
for (unsigned int i = 0; i < len; ++i)
{
if (a_bstr[i] != '\n')
wide_str += a_bstr[i];
else
break;
}
// wide_str holds your first line
std::wcout << "First line: " << wide_str << std::endl;
::SysFreeString(a_bstr);
return 0;
}https://stackoverflow.com/questions/33040278
复制相似问题