【问题标题】:String stream related problems字符串流相关问题
【发布时间】:2013-11-25 12:37:35
【问题描述】:

我正在尝试执行以下操作

std::stringstream str;
string  string1, string2;
long a = 23343;
long b = 323234;
str << std::hex << a;
str >> string1;       //line no. 6
str << std::hex << b;
str >> string2;       //line no.8

a & b 是包含在结构中的值,该结构作为事件发送到其他进程,该进程提取这些值。 我遇到的问题是当我这样做时

str.flush()

在第 6 行之后,string string1 为空,一个空的string 被传递给其他进程。

有没有办法将stringstream str 重用于不同的值?

这是实际的代码——

void* CIAppDiagService::SendDidValue(void *arg)
{

DiagService::SReadDIDResponse didResponse;
didResponse.ComponentId = ::DiagService::eComponentMin;  //Todo
didResponse.bStatus=false;
didResponse.nDIDCode=((CIAppDiagService*)arg)->mDIDCode;


if(didResponse.nDIDCode ==  0xD95C)
{
    ::CIApplicationManager::UUIDList_var aUidList;
    //get uuid list
    AppMgrServerData::getAppMgrServerData()->AppDirectory().getList(aUidList.out()); //fills the UUIDList which is a sequence of string

    if(aUidList->length() > 0)
    {
        std::string aAppDetails,aAppVersion,aAppVersionString,aAppIDString,NumberOfAppsInstalledString;
        AppId aAppId;   //typedef long AppId
        CORBA::ULong ulength = aUidList->length();
        std::stringstream stream,stream1,stream2;
        short int iAppVersion;
        aAppDetails.append("000000ff");
        short int iNumberOfAppsInstalled = AppMgrServerData::getAppMgrServerData()->AppDirectory().getTotalAppsInstalled();  // returns and integer
        stream << std::hex << iNumberOfAppsInstalled;
        stream >> NumberOfAppsInstalledString;
        stream.clear();
        if(1 == NumberOfAppsInstalledString.length() )
        {
            NumberOfAppsInstalledString = "000" + NumberOfAppsInstalledString;
        }
        else if(2 == NumberOfAppsInstalledString.length() )
        {
            NumberOfAppsInstalledString = "00" + NumberOfAppsInstalledString;
        }
        else if(3 == NumberOfAppsInstalledString.length() )
        {
            NumberOfAppsInstalledString = "0" + NumberOfAppsInstalledString;
        }
        strcat(const_cast<char*>(aAppDetails.c_str()),NumberOfAppsInstalledString.c_str());

        for(int count = 0; count< aUidList->length(); count++)
        {
                        aAppId = AppMgrServerData::getAppMgrServerData()->AppDirectory().getApplicationAppIDByUid((aUidList[count]));
            aAppVersion = AppMgrServerData::getAppMgrServerData()->AppDirectory().getAppVersion(string(aUidList[count]));


            stream1 << std::hex << aAppId % (std::numeric_limits<uint16_t>::max()+1);


            stream1 >> aAppIDString; //convert aAppID into string

            stream1.clear();

            strcat(const_cast<char*>(aAppDetails.c_str()),aAppIDString.c_str());
            TRACE(cout<<"check 1"<<endl);
            iAppVersion = atoi(aAppVersion.c_str());
            stream2 << std::hex << aAppVersion;
            TRACE(cout<<"CIAppDiagService::: SendDidValue::: aAppVersion stream = "<< stream2 << endl);
            stream2.seekg(0, std::ios::beg);
            stream2 >> aAppVersionString;

            stream2.clear();

//The following is some restriction/implementation that i need to follow.

            if(1 == aAppVersionString.length() )
            {
                aAppVersionString = "000" + aAppVersionString;
            }
            else if(2 == aAppVersionString.length() )
            {
                aAppVersionString = "00" + aAppVersionString;
            }
            else if(3 == aAppVersionString.length() )
            {
                aAppVersionString = "0" + aAppVersionString;
            }
            strcat(const_cast<char*>(aAppDetails.c_str()),aAppVersionString.c_str()); //aAppVersion concatenated

        }
        int length = strlen(aAppDetails.c_str()) + 1;
        if(length)
        {
            didResponse.DIDValue.length(length);
            for(int i=0; i < length; i++)
            {
                didResponse.DIDValue[i]=aAppDetails[i];
                cout<<" CIAppDiagService::SendDidValue:: didResponse.DIDValue[i]"<<didResponse.DIDValue[i]<<endl;
            }
        //  didResponse.DIDValue[length] = '\0';
            didResponse.bStatus = true;
        }
        else
        {
            char errResult[]={0x7F,0x22,0x22};  //error codes from media team/to be changed
            didResponse.DIDValue.length(3);
            didResponse.bStatus=false;

            for(int i=0; i<3; i++)
            {
                didResponse.DIDValue[i]=errResult[i];
            }
        }
            ((CIAppDiagService*)arg)->SendEvent_ResponseDidValue(didResponse);
    }
    else
    {
        cout<<" No applications installed/No AppIDs found. "<<endl;
    }
}

return 0;

}

void CIAppDiagService::SendEvent_ResponseDidValue(DiagService::SReadDIDResponse didResponse)
{
TRACE(cout<<"CIAppDiagService::entered in Send_Event Response"<<endl);
CosNotification::StructuredEvent event;
event.header.fixed_header.event_type.domain_name = CORBA::string_dup(DiagService::gDiagnosisServiceDomain);
event.header.fixed_header.event_name = CORBA::string_dup(DiagService::gReadDIDEventName);
event.header.variable_header.length(0); // put nothing here
event.filterable_data.length(0);

 DiagService::SReadDIDResponse *tmp = new DiagService::SReadDIDResponse;
 if (tmp != NULL) {
    tmp->ComponentId = didResponse.ComponentId; //TODO
    tmp->DIDValue = didResponse.DIDValue;
    tmp->bStatus = didResponse.bStatus;
    tmp->nDIDCode = didResponse.nDIDCode;

    event.remainder_of_body <<= *tmp;
    TRACE(cout<<"CIAppDiagService::SendEvent_ResponseDidValue:: calling send event"<< endl);
    mCIDiagSupplier->send_event(event);
 }

TRACE(cout<<"CIAppDiagService::exited from Send_Event Response"<<endl);

}

想法是将所有字符串连接成一个字符串appDetails并填充结构成员SReadDIDResponse.DIDValue 这是结构 SReadDIDResponse

struct SReadDIDResponse {
EComponent ComponentId;   //enum values
TID nDIDCode;             // integer
OctetSeq DIDValue;       //octet sequence
boolean bStatus;
};

【问题讨论】:

  • 您没有在代码中显示flush。没有它可以工作吗?
  • @crashmstr——不,先生。没有刷新,同样的事情仍然存在。
  • 你为什么不string1 = str.str()
  • 运行时错误是否发生在您发布的代码中(即在stringstream 操作中)?
  • 实际上我将它转换为十六进制,然后将其放入字符串中。例如。 str > string1;

标签: c++ string corba strstream


【解决方案1】:

使用str.clear()在第6行之后重置字符串流。

Problem with re-using a stringstream object

【讨论】:

  • @user2559758 那么您能否发布可以 重现您的问题的代码?最好有详细的描述。您现有的代码在添加str.clear() 后可以正常工作,至少在我的笔记本电脑上是这样。
  • @user2559758 确实如此,这正是 sstream 不能被重用的原因(eof-bit 是在第一次输出到 string1 之后设置的)。
【解决方案2】:

我认为您需要在从中提取数据之前倒带流:

str.seekg(0, std::ios::beg);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-12
    • 2022-12-20
    • 2020-07-03
    • 1970-01-01
    • 2023-03-15
    • 2012-07-05
    • 1970-01-01
    相关资源
    最近更新 更多