Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading from a very large text file resource in C++

We have some data in a text file which is built into our executable as a custom resource to be read at runtime. The size of this text file is over 7 million characters.

I can successfully search for and locate strings within the resource which appear near the top of the text file, but when attempting to search for terms a few million characters down, strstr returns NULL indicating that the string cannot be found. Is there a limit to the length of a string literal that can be stored in a char* or the amount of data that can be stored in an embedded resource? Code is shown below

char* data = NULL;
HINSTANCE hInst = NULL;
HRSRC hRes = FindResource(hInst, MAKEINTRESOURCE(IDR_TEXT_FILE1), "TESTRESOURCE");
if(NULL != hRes)
{
    HGLOBAL hData = LoadResource(hInst, hRes);
    if (hData)
    {
        DWORD dataSize = SizeofResource(hInst, hRes);
        data = (char*)LockResource(hData);
    }
    else
        break;

    char* pkcSearchResult = strstr(data, "NumListDetails");
    if ( pkcSearchResult != NULL )
    {
        // parse data
    }
}

Thanks.

like image 624
Bill Walton Avatar asked Jan 25 '26 19:01

Bill Walton


1 Answers

The problem might be the method you use for searching. strstr uses ANSI strings, and will terminate when it encounters a '\0' in the search domain.

You might use something like memstr (one of many implementations can be found here).

like image 83
Peter Hübel Avatar answered Jan 27 '26 08:01

Peter Hübel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!