2013-07-26 44 views
0

我試圖在給定文件中找到一個字符串(實際上該文件是tar文件(請注意這裏),並且我在記事本++中打開該文件並隨機抽取從打開的文件中得到一個字符串),然後我將這個完整的tar文件存儲在一個緩衝區中,現在我想查找使用存儲緩衝區中的strstr函數複製的字符串的位置。strstr無法搜索八進制格式數據中的字符串

做的代碼是這樣的(這是絕對正確的) -

char *compare= "_.png"; //suppose this is the copied string 
//which is to be find out in buffer using strstr 
      char * StartPosition; 
      StartPosition = strstr (buffer,compare); 
      __int64 count=0; 
      MessageBox(m_hwndPreview,L"before the while loop",L"BTN WND6",MB_ICONINFORMATION); 
      while (StartPosition!=NULL) 
      { 
       MessageBox(m_hwndPreview,L"hurr inside the while loop",L"BTN WND6",MB_ICONINFORMATION); 
       MessageBoxA(m_hwndPreview,strerror(errno),"BTN WND4", MB_ICONINFORMATION); 
       count=StartPosition-buffer+1; 
       return 0; 
      } 

,並假設如果我有tar文件的內容在記事本一樣從我複製此字符串存儲在compare-

如下
3_VehicleWithKinematicsAndAerodynamics_.000.png IHDR (here is some strange data which can't be copied and also there are lot of NULL but we have to find out the position of "_.png" so not so difficult in this case). 

的問題是我的代碼運行正常,直到我的巴紐之前的數據存儲,然後我能夠找到使用的的strstr問題的立場是,當我試圖找出哪些是

0之後出現的字符串現在的位置
`3_VehicleWithKinematicsAndAerodynamics_.000.png IHDR ...suppose here we have strange data (which is data block if we see the tar parser)...after this we have another file like..."3_VehicleWithKinematicsAndAerodynamics_.html"` 

,如果我想找到這個「3_VehicleWithKinematicsAndAerodynamics_.html」用那麼的strstr我不能夠找到它,因爲他們之間的奇怪的數據。(因爲我認爲這些數據不會被編譯器和DUT認可對,我不能夠訪問位於奇怪的數據後) 作出更加明確的文件中看到的tar文件文件的位置如下 -

3_VehicleWithKinematicsAndAerodynamics_.000.png IHDR ............(its data blocl-which is strange contents if you open in tar file)....3_VehicleWithKinematicsAndAerodynamics_.000.html 

我要訪問.html文件使用strstr。爲什麼它不訪問它?有任何想法嗎 ?? *

請給替代方案,以實現IT..I我知道我嘗試將不會工作。

+3

我相對確定'strstr'將停在它遇到的第一個'\ 0'字節處。 – arne

+0

您試圖在原始二進制數據上使用字符串函數 - 這肯定不會像您想要的那樣運行 –

+0

您是否有解決此問題的方法?任何其他選擇? – Sss

回答

2

AC風格字符串是一個由數零字符終止字符(NULL字符 - 值零,而不是字符'0')。這意味着strstr只要碰到這樣一個字節就會停止。

一個比較合理的解決方案是簡單地編寫一個函數,該函數根據長度而不是「終止字符」搜索二進制數據。

像這樣的東西(這還是假定str是C風格的字符串):

char *find_str_in_data(const char *str, const char *data, int length) 
{ 
    int pos = 0; 
    int slen = strlen(str); 
    while(pos < length-slen) 
    { 
     int i = 0; 
     while(i < slen && str[i] = data[pos+i]) 
     { 
      i++; 
     } 
     if (i == slen) 
      return data + pos; 
    } 
    return NULL; 
} 
+0

但是我必須在visual C++中做,並且在兩個文件之間不僅有NULL,而且還有像「™< Cx9ð¾「2RDF Dèqq‡hðýèÑ(ÎïC...¹\ XX 7¥~þªøý」0 ...BÄKƒ...ßcB!Ôbí ñå&½¾>Åk»0¡£½Æ<½©Ø3E...Ntâõ!同時也存在這樣的問題:HH¯A A5454 l a a a TB TB TB TB a a a a a a a a a a a a a a a TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB TB are are are are are are are are are are are are are are are are are如何訪問位於這個奇怪數據後面的第二個文件 – Sss

+0

上面的代碼應該能夠找到字符串,如果它存在的話,無論其他數據如何。要進一步查找另一個字符串,只需從最後找到的點開始(並且相應地調整長度)即可。儘管我沒有在該字符串中看到任何'.png'。 –

+0

該tar文件包含像3_VehicleWithKinematicsAndAerodynamics_.000.png(它在開始時)...在奇怪的數據和NULL之間,然後.... 3_VehicleWithKinematicsAndAerodynamics_.html(我必須知道它的位置後從這裏開始加載數據)。所以beofre我的意思是說,strstr能夠找到字符串的位置,直到.png(在開始文件的末尾有文件擴展名爲「.png」),在這個.png之後有奇怪的數據,所以我無法訪問第二個文件的內容(這實際上是一個.html文件,我必須將它存儲在緩衝區中)undrstod我? – Sss

0

如果你真的想使用strstr,那麼你需要逃避包含在緩衝區'\0'的字符串。如果你知道被放入緩衝數據的大小(比方說,sizeOfData),比使用strstr之前,你可以做這樣的事情:

buffer[sizeOfData] = '\0'; 

警告:如果sizeOfData等於大小的緩衝區,那麼你需要更大的緩衝區或覆蓋最後一個字符'\0'(在第二種情況下,你應該手動檢查緩衝區尾部,因爲你覆蓋的字符可能是你正在尋找的序列中的一個字符)。