如何循环BSTR
How to loop a BSTR?
我有一个包含大量字符的BSTR。
BSTR theFile = NULL;
int res = comSmartspeak -> readTheFile(fileName, &theFile);
我想读第一行,但我不知道怎么做。
这是我想出来的伪代码:
string firstLine = "";
for (int i = 0; i < SysStringLen(theFile) ; i++)
{
if (theFile[i] == 'n')
{
break;
}else
{
firstLine += theFile[i] ;
}
}
我是vc++新手
//another option
BSTR comStr = ::SysAllocString(L"First line of textnSecond line of textnThird line of textnFourth 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);
}
您可以这样做,假设您可以自由使用std::wstring,这样您就不必事先计算第一行的长度:
#include <Windows.h>
#include <iostream>
#include <string>
int main()
{
BSTR a_bstr = ::SysAllocString(L"First line of textnSecond line of textnThird line of textnFourth 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;
}
相关文章:
- 如何循环打印顶点结构
- 如何在C++中从两个单独的for循环中添加两个数组
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 正在尝试了解输入验证循环
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 循环后如何继续阅读
- Ardunio UNO解决了多个重叠的定时器循环
- Eigen如何在容器循环中干净地附加矩阵
- 在某些循环内使用vector.push_back时出现分段错误
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 为什么我的for循环不能正确获取argv
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- while循环中while循环的时间复杂度是多少
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 为什么在这个代码结束循环中没有得到结束
- 在基于范围的for循环中使用结构化绑定声明
- 用于C++中带有数组和指针的循环
- 循环中的随机函数
- 循环中的BSTR字符串操作
- 如何循环BSTR