可能不是您正在寻找的答案,但这里最好的解决方案可能是正确初始化您的字符串和指针。如果你的记忆里有垃圾,何不做点正经事,设置好
yourString[0] = '\0';
如果它真的只是一个任意位的缓冲区,您最好使用memcmp 之类的东西,然后沿着N 字符滑动内存缓冲区的指针(其中N 是您感兴趣的字符数减去您要比较的字符串的长度)。这可能不是最有效的实现,但我应该认为应该是一种相当稳健的方法。
[编辑]你的问题引起了我的兴趣,我做了一些实验。鉴于您似乎正在寻找更多 C 风格的答案,这里有一些我想出的代码 sn-p 来详细说明我的 memcmp 建议:
// SearchingMemoryForStrings.cpp : Defines the entry point for a win32 consol application
// Purpose : Demonstrates a way to search a section of memory for a particular string
//
#include <stdio.h>
#include <string.h>
#define VALUE_NOT_FOUND (-1)
int FindStringInBuffer( const char* pMemBuffer, const size_t& bufferSizeInBytes, const char* pStrToFind )
{
int stringFound = VALUE_NOT_FOUND; // Return value which will be >= 0 if we find the string we're after
const char* pMemToMatch = NULL; // An offset pointer to part of 'pMemBuffer' which we'll feed to memcmp to find 'pStrToFind'
// Set up some constants we'll use while searching
size_t lenOfStrToFind = strlen( pStrToFind );
size_t lastSearchablePosition = bufferSizeInBytes - lenOfStrToFind;
// Search the memory buffer, shifting one character at a time for 'pStrToFind'
for( size_t i = 0; i <= lastSearchablePosition; i++ ) {
pMemToMatch = &pMemBuffer[i];
if( memcmp(pMemToMatch, pStrToFind, lenOfStrToFind) == 0 ) {
// We found the string we're looking for
stringFound = i;
break;
}
}
return stringFound;
}
void ReportResult( int returnVal, const char* stringToFind )
{
if( returnVal == VALUE_NOT_FOUND ) {
// Fail!
printf("Error, failed to find '%s' - search function returned %d\n", stringToFind, returnVal );
}
else {
// Win!
printf("Success, found '%s' at index %d\n", stringToFind, returnVal );
}
}
void FindAndReport( const char* pMemBuffer, const size_t& bufferSizeInBytes, const char* pStrToFind )
{
int result = FindStringInBuffer( pMemBuffer, bufferSizeInBytes, pStrToFind );
ReportResult( result, pStrToFind );
}
int main( int argc, char* argv[] )
{
const int SIZE_OF_BUFFER = 1024; // Some aribitrary buffer size
char some_memory[SIZE_OF_BUFFER]; // The buffer of randomly assigned memory to look for our string
const char* stringToFind = "This test should pass";
const char* stringYouWontFind = "This test should fail";
FindAndReport( some_memory, SIZE_OF_BUFFER, stringYouWontFind ); // Should fail gracefully
// Set the end of the buffer to the string we're looking for
memcpy( &some_memory[SIZE_OF_BUFFER-strlen(stringToFind)], stringToFind, strlen(stringToFind) );
FindAndReport( some_memory, SIZE_OF_BUFFER, stringToFind ); // Should succeed this time and report an index of 1003
// Try adding at some arbitrary position
memcpy( &some_memory[100], stringToFind, strlen(stringToFind) );
FindAndReport( some_memory, SIZE_OF_BUFFER, stringToFind ); // Should still succeed but report the offset as 100
FindAndReport( some_memory, SIZE_OF_BUFFER, stringYouWontFind ); // Should still fail
return 0;
}
在 Visual Studio 2008 下编译为 Win32 控制台应用程序的 sn-p。给我以下内容:
Error, failed to find 'This test should fail' - search function returned -1
Success, found 'This test should pass' at index 1003
Success, found 'This test should pass' at index 100
Error, failed to find 'This test should fail' - search function returned -1
FindStringInBuffer 函数是您想要的,如果您需要处理宽字符,则需要进行一些转换,但这至少应该为您提供一些可以继续进行的想法。如果您确实提出了 wchar 版本,我很想看看解决方案是什么样的(我自己没有处理过它们)。