2013-07-30 59 views
0

難道你們中的任何一個能幫我在這一行使用sscanf獲得整數(層,ieta,深度)嗎?我在(1)之前使用了一個單獨的函數,它工作正常。但我只想嘗試一些新的東西(2),程序對每個變量返回0。使用sscanf解析連續字符串

(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); 

回答

0

你不能初始化像這樣的字符串:

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

它需要:

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