首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何循环BSTR?

如何循环BSTR?
EN

Stack Overflow用户
提问于 2015-10-09 14:01:57
回答 2查看 321关注 0票数 1

我有一个BSTR,里面有很多的混血儿。

代码语言:javascript
复制
        BSTR theFile = NULL;
        int res = comSmartspeak -> readTheFile(fileName, &theFile);

我想读第一行,但我不知道怎么做。

这是我想出的pseudoCode:

代码语言:javascript
复制
        string firstLine = "";
        for (int i = 0; i < SysStringLen(theFile) ; i++)
        {
            if (theFile[i] == '\n')
            {
                break;
            }else
            {
                firstLine += theFile[i] ;
            }
        }

我是VC++的新手。

EN

回答 2

Stack Overflow用户

发布于 2015-10-09 15:36:13

代码语言:javascript
复制
//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);
    }
票数 1
EN

Stack Overflow用户

发布于 2015-10-09 14:40:30

您可以这样做,前提是您可以自由地使用std::wstring,这样您就不必事先计算第一行的长度:

代码语言:javascript
复制
#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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33040278

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档