Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching for a string in a file

Status
Not open for further replies.

emperorevil

Programmer
Apr 28, 2002
23
US
How would I go about seeing if a certain string is in a file? I tried reading the file, turning it into a CString by doing:

CString file2(file);

then trying

check=file2.Find("string");

the file has the string "string" in it, but it always comes up as not finding it.. can you help?
 
The problem must be in converting the contents of the file into a CString
 
so how would i go about changing a char[] into a CString?
 
no.. that all works.. its something wrong with the actual file. The actual file is a .exe and i tested it with a regular text file with the contents "fdjstringkj" and it came out true, but when i try the exe file, which has the string, it doesnt come up! It must be when the char converts to a CString, it stops at a certain point. When I output the CString file2, all i get is hte first 3 letters of the file.

Should I not convert it to a CString?
 
Of course it stops too early, because an .exe file contains a lot of NULL-characters, which make a string end.
I think you should use the API CreateFile to open the file, followed by GetFileSize to get the actual filesize, followed by malloc to create a buffer large enough to contain the entire file, followed by ReadFile, reading the entire file into the buffer. You can call CloseHandle immediately, search for the string and then call free to free the buffer.
It is also possible, if the string is from a resourcefile, the actual string in the .exe is unicode. This is a bit harder to find. If you want to search for "ABC" you probably have to search for "A\0B\0C\0" if it is unicode.

Marcel
 
Would you mind giving me some code i can work with? I'm a beginning Visual C++ Programmer..:(
 
Here it is



#include <windows.h>
#include <stdio.h>

// fEqual returns TRUE if pStr1 is equal to pStr2 over a distance of
// dwLength bytes, otherwise FALSE
// Input parameter : fUpperIsLower --> if TRUE &quot;ABC&quot; == &quot;abc&quot;
// if FALSE &quot;ABC&quot; != &quot;abc&quot;

BOOL fEqual ( char *pStr1,
char *pStr2,
DWORD dwLength,
BOOL fUpperIsLower )
{
if ( !dwLength ) return TRUE; // Length 0 --> TRUE
if ( pStr1 == pStr2 ) return TRUE; // Same pointer --> TRUE
if ( pStr1 == NULL || pStr2 == NULL ) return FALSE;
if ( fUpperIsLower )
{ for ( DWORD i1 = 0; i1 < dwLength; i1++ )
{ if ( toupper ( *(pStr1 + i1 )) != toupper ( *(pStr2 + i1 )) )
{ return FALSE; } }
} else
{ if ( memcmp ( pStr1, pStr2, dwLength ) != 0 )
{ return FALSE; } }
return TRUE; }

// FindStringInFile searches for the presence of a string in a file.
// Return values 0xffffffff means the string is not found or an error
// occurred.
// Otherwise the return value is the offset of the first occurrence of
// the string in the file.
// Input parameter : fUpperIsLower --> if TRUE &quot;ABC&quot; == &quot;abc&quot;
// if FALSE &quot;ABC&quot; != &quot;abc&quot;


DWORD FindStringInFile ( char *pFileName,
char *pSearchString,
DWORD cbSearchString,
BOOL fUpperIsLower )
{
if ( pFileName == NULL ) return 0xffffffff;
if ( pSearchString == NULL ) return 0xffffffff;
if ( !cbSearchString ) return 0xffffffff;

DWORD dwReturnValue = 0xffffffff;
char *Buffer = NULL;
HANDLE fh = INVALID_HANDLE_VALUE;

__try
{
// Open file for reading it

fh = CreateFile ( pFileName, GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL );

// Is the file open?

if ( fh == INVALID_HANDLE_VALUE ) __leave;

// Get the file size

DWORD dwFileSize = GetFileSize ( fh, NULL );

// Did GetGileSize succeed?

if ( dwFileSize == 0xffffffff ) __leave;

// Is the filesize less than the size of the searchstring?

if ( dwFileSize < cbSearchString ) __leave;

// Allocate memory to read the entire file at once

Buffer = (char *)( malloc ( dwFileSize ));

// Memory allocation succesful?

if ( Buffer == NULL ) __leave;

// Read file in buffer

DWORD dwBytesRead;
if ( !ReadFile ( fh, Buffer, dwFileSize, &dwBytesRead, NULL )) __leave;

// Close file

CloseHandle ( fh );
fh = INVALID_HANDLE_VALUE;

// Is the searchstring greter than the number of bytes read ?

if ( cbSearchString > dwBytesRead ) __leave;

// calculate the last offset to be compared

DWORD dwOffsetLast = dwBytesRead + 1 - cbSearchString;

// Search for the string

for ( DWORD dwOffset = 0; dwOffset < dwOffsetLast; dwOffset++ )
{ if ( fEqual ( Buffer + dwOffset, pSearchString,
cbSearchString, fUpperIsLower ))
{ dwReturnValue = dwOffset;
__leave; } }


}

__finally
{ if ( Buffer != NULL )
{ free ( Buffer );
Buffer = NULL; }
if ( fh != INVALID_HANDLE_VALUE )
{ CloseHandle ( fh );
fh = INVALID_HANDLE_VALUE; }
return dwReturnValue; }

// the next line should never be executed but is there to prevent a
// compiler warning &quot;Not all control paths return a value&quot;

return dwReturnValue; }



void main ( )
{
char FileName[MAX_PATH];
char UpIsLow[10];
char SearchString[MAX_PATH];

printf ( &quot;File name : &quot; ); scanf ( &quot;%s&quot;, &FileName );
printf ( &quot;Search for : &quot; ); scanf ( &quot;%s&quot;, &SearchString );
printf ( &quot;A == a (Y/N) : &quot; ); scanf ( &quot;%s&quot;, &UpIsLow );

BOOL fUpIsLow = toupper ( UpIsLow[0] ) == 'Y' ? TRUE : FALSE;

DWORD dwOffset = FindStringInFile ( FileName, SearchString,
strlen ( SearchString ),
fUpIsLow );

if ( dwOffset == 0xffffffff )
{ printf ( &quot;String %s not found in file %s\n&quot;,
SearchString, FileName ); } else
{ printf ( &quot;String %s found at offset %d in file %s\n&quot;,
SearchString, dwOffset, FileName ); } }



Marcel


 
HURRAY! It works!
Just one more problem though..

Since my program is on a dialog box, and the text the user inputs is in a text box, and that data is stored as a CString..

How do I convert a CString into a character array and in vice versa?
 
It has implicit conversions to support strcpy etc or you could call GetBuffer

Matt
 
nevermind I was able to make it work by changing

DWORD FindStringInFile( char *pFileName...

to LPCTSTR pFileName, and then converting the CString variable, m_dir, which holds the directory of the file being searching that was inputted by the user, into a LPCTSTR.

LPCTSTR FileName=LPCTSTR(m_dir);

Yes I rule! Thank you!
 
Okay, now, how would I go about reading and writing(overwriting) within a specific location this way?
 
NEVERMIND again, I forgot that I don't think I need special code to read and write files -_-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top