【问题标题】:parsing continuous string using sscanf使用 sscanf 解析连续字符串
【发布时间】:2013-07-30 11:00:50
【问题描述】:

你们可以帮我使用 sscanf 获取这一行中的整数(层、ieta、深度)吗?我在(1)之前使用了一个单独的函数并且它有效。但我只是想尝试一些新的东西(2),程序为每个变量返回 0。

(1)

Int_t getIetaFromHistName (string histName)
{

Int_t IetaPos = histName.find("_Ieta"); //find position of Ieta & return as integer
Int_t IphiPos = histName.find("_Iphi");
return atoi(histName.substr(IetaPos+5,IphiPos-IetaPos-5).c_str()); //reconstruct the string (post of 1st char to be copied, string length)

};

(2)

char histoName [100] = event2->GetName();
sscanf(histoName,"H2_HB_PHI12_LAYER%d_SRCTUBE_Ieta%d_Iphi12_Depth%d",&layNo,&ietaNo,&depthNo); //string histoName = H2_HB_PHI12_LAYER10_SRCTUBE_Ieta1_Iphi12_Depth1
printf("Layer= %d , Ieta= %d , Depth= %d\n",layNo,ietaNo,depthNo);

【问题讨论】:

    标签: c scanf


    【解决方案1】:

    你不能像这样初始化一个字符串:

    char histoName [100] = event2->GetName();
    

    必须是:

    char histoName [100];
    strcpy(histoName, event2->getName());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-28
      • 1970-01-01
      • 2013-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-10
      • 2021-11-18
      相关资源
      最近更新 更多