A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
Searching for a string in a FileLet's you find the first occurance of a string within a file. |
On Sunday, April 25th 2004 at 12:46 PM By Sean Eshbaugh (View Profile) ![]() ![]() ![]() ![]() (Rated 4.3 with 23 votes) |
||
|
I started working on a project that was to replace the often times buggy and slow (and in my opinion just plain bad) Find Files/Folders function that comes with Windows (windows key + 'F'). In Windows XP the searching utility in the OS seems to be severly lacking in functionality. In previous versions of Windows I used i didn't find there to be too many problems.
unsigned long FileSearch(FILE* pFile, const char* lpszSearchString)
Just a note, I know the return value is unsigned and in all the error cases I returned -1, remember, -1 is the same as 0xFFFFFFFF in a 32-bit number. Since i sincerly doubt you will ever come across a single file that is over 4GB this should never be a problem. If you should need to search a file that is over 4GB then I suggest replacing "unsigned long" with "unsigned __int64" if your compiler supports it. If you do need to do that then I doubt even more your hard drive can even hold a file that is 2^64 bytes in size so returning -1 (a REALLY big number for 64-bit numbers) will do nicely. The above code is probably not the most effecient way of doing this, but it works, and it works fast. If i get the time I might try and make this as fast as possible, but unless this becomes the bottleneck of a program I'm working on that might not be for a while. |
|||
Digg It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
|||
|
|||
Current CommentsHi sean,
I am quite intreseted in you work.You have done a very good job.I would like to have full code of this program can u send it to my mail-id please . bsuman256@rediffmail.com is my id..
I would be very thankful to you for sharing your C code of seraching a string in a file.
Thank You very much in advance
Suman Bharath.
It si good pice of work. Could u send me the complete copy of the C++ code u have written.My email id being kittu24@gmail.com
Regards
KS
Thanks for ur code .If u didnt posted it i too gone for implementing ... this code is really nice thank u.
hi, It is really good. Could u send me the full code, to my id, satya.vijai@gmail.com.
Thanks in advance
Hi Sean,
Thanks for sharing your code with us. It\'s pretty neat and I found it easy to understand.
If its not too much trouble, could you please send me the full code to skemii@hotmail.com?....you don\'t have to if you don\'t want to though.
Happy programming! & Thanks again!
Hi Sean,
This is excellent. I was looking for something like for a while. We have a issue where I need to find string with file but couldn\'t find anything. Thanks for great work.
I have one question and it might be obvious to all but not me:( how do I run this code if I am searching for string\"RT5004\" with a file and I have over 10,000 plus files.
Thanks,
Sami
Hi sean
I want to work with file handling.so can u please send me full code ??
waiting for your reply.
Thanks & Regards
Vineeta
hey man... It is too good. Can u send me the full code to my id !!! :) leoviveke@yahoo.com
Thanks in advance
Your program is absolutely working fine.
But ther are cases where the results are not as required.
For ex: I need to find a string "if" in some file.
The words that contain the word if is also considered valid, which should not be.
i.e "theif" this word contains if... this is also taken into consideration.
can u suggest me a better way to avoid this fact.
i need to search for a string in a pdf file using C#.Net and Asp.Net , could u help me please
Hi i try to run your code into MFC application,But there i faced some err like cannot convert parameter 1 from \'struct _iobuf *\' to \'char *\'.so please send a full source code to me
Hi very fantastic job done yar... Can i get a full source code...?
Fantastic bit of code, helped me enormously in a project im working on.
I would say change:
while (ulCurrentPosition<ulFileSize-ulBufferSize)
to:
while (ulCurrentPosition<=ulFileSize-ulBufferSize)
without it i was missing the last character of the file off therefore if the required string was there it wasn\'t found.
For purposes of speed here's a version that only reads each character from file once (and thus doesn't need the extra seeks). I'm using it for a true/false on whether the string occurs but the filePos variable has the right value to be returned instead of true to use this as a search for first occurrence method.
The key is to use the same buffer and keep shifting the bytes to make room for more. I use a 2n-1 buffer so that each byte is only moved in memory once.
//Use this signature with obvious changes to find position in file of first occurrence
//static unsigned long findStrInFile(FILE* pFile, const char* str)
inline bool fileContainsStr(FILE* pFile, const char* const str)
{
const unsigned long strLen = strlen(str), strLenM1 = strLen-1;
if( !str || !strLen || !pFile ) { return false; }
if( fseek(pFile, 0, SEEK_END) != 0 ) { return false; }
unsigned long fileLen = ftell(pFile); fseek(pFile, 0, SEEK_SET);
if( !fileLen || strLen > fileLen ) { return false; }
char* const searchBuf = (char*)malloc( 2*strLen - 1 ); if( !searchBuf ) { return false; }
char *pSearch = searchBuf, *pWrite = searchBuf, * const pMid = searchBuf strLenM1;
unsigned long filePos = 0;
fread(searchBuf, strLenM1, 1, pFile); pWrite = strLenM1;
while( 1 )
{
fread(pWrite, 1, 1, pFile); pWrite;
if( !memcmp( pSearch, str, strLen ) ) { free(searchBuf); return true; }
if( filePos > fileLen - strLen ) { break; }
if( pSearch > pMid ) { memcpy(searchBuf, pSearch, strLenM1); pSearch = searchBuf; pWrite = pMid; }
}
free(searchBuf); return false;
}
pMid should be initialized to searchBuf PLUS strLenM1, seems to have dropped the " " on copy or paste somewhere. Also very importan that filePos > fileLen - strLen test should be precedeed by thre prefix increment operator \ \ , " "" ". plus plus. Not sure why my plus signs vanished. pSearch is also pre-incremented before comparison to pMid.
For cur/pasteability
<pre>
For purposes of speed here's a version that only reads each character from file once (and thus doesn't need the extra seeks). I'm using it for a true/false on whether the string occurs but the filePos variable has the right value to be returned instead of true to use this as a search for first occurrence method.
The key is to use the same buffer and keep shifting the bytes to make room for more. I use a 2n-1 buffer so that each byte is only moved in memory once.
//Use this signature with obvious changes to find position in file of first occurrence
//static unsigned long findStrInFile(FILE* pFile, const char* str)
inline bool fileContainsStr(FILE* pFile, const char* const str)
{
const unsigned long strLen = strlen(str), strLenM1 = strLen-1;
if( !str || !strLen || !pFile ) { return false; }
if( fseek(pFile, 0, SEEK_END) != 0 ) { return false; }
unsigned long fileLen = ftell(pFile); fseek(pFile, 0, SEEK_SET);
if( !fileLen || strLen > fileLen ) { return false; }
char* const searchBuf = (char*)malloc( 2*strLen - 1 ); if( !searchBuf ) { return false; }
char *pSearch = searchBuf, *pWrite = searchBuf, * const pMid = searchBuf strLenM1;
unsigned long filePos = 0;
fread(searchBuf, strLenM1, 1, pFile); pWrite = strLenM1;
while( 1 )
{
fread(pWrite, 1, 1, pFile); pWrite;
if( !memcmp( pSearch, str, strLen ) ) { free(searchBuf); return true; }
if( filePos > fileLen - strLen ) { break; }
if( pSearch > pMid ) { memcpy(searchBuf, pSearch, strLenM1); pSearch = searchBuf; pWrite = pMid; }
}
free(searchBuf); return false;
}
</pre>
I need your help sir.will you have create search programming in c . you have the send the program my mail id.
Thanking you,
I need your help sir.will you have create search programming in c . you have the send the program my mail id.
Thanking you,
I want to parse a log file for text
like say string starting with ABC and ending till first coccurance of ; after that
i want all such string in a seperate file
can you please help
thanks
Its a really good illustration of string searching.
Can u send me the full source code on saurabh_717@yahoo.co.in
so i can further work on it.
Hi, can you send this source code my mail id : msmohanbabu@gmail.com.
Thanks
For string searches use the Knuth-Morris-Pratt algorithm. It's faster!
http://en.wikipedia.org/wiki/Knuth-Morris-Pratt_algorithm
Hi,
@Merlin interesting code, unfortunately I
have some troubles getting it compiled:
When compiling under VS 2008 I get an error in the line: fread(searchBuf, strLenM1, 1, pFile); pWrite = strLenM1;
error C2440: '=' : cannot convert from 'const unsigned long' to 'char *'
and secondly what does the pWrite does after this:
fread(pWrite, 1, 1, pFile); pWrite;
Thanks!
Nice job Sean. Thanks.
REally good code....
hi!! everyone, can someone help me with this problem. I have been asked to do a function to search for id's of students given a text file which should I use within. Id is string so you must return a id in string.
This is a very good piece of code. Could you send me the full source at nasthalgic@yahoo.com
Thanks you very much. Great Work!!!
This is a very good piece of code. Could you send me the full source at nasthalgic@yahoo.com
Thanks you very much. Great Work!!!
could you send me also the full source code of it.... thank you sooooooooo much!!! bhedoca@yahoo.com ^^,
This look really great, i'm doing my degree in computer science and this is my first year.would be very helpfull if i could get the full source code(nick_30266@hotmail.com)? Thaaaankssss
This look really great, i'm doing my degree in computer science and this is my first year.would be very helpfull if i could get the full source code(nick_30266@hotmail.com)? Thaaaankssss
Hello,
Congratulations with this fine piece of code. Would you be so kind to share the code with me?
Thanks a lot in advance
Hi!!
I need to write a program that writes book data like name, publisher, author, price and stock into a file. Then search the same file for a given book name entered by user. If book is found, display its data, otherwise if out of stock then report the same.
Please help me how to find a particular book name in the file. Rest of the program is done, only problem is searching!!!!
Help!!!!!
Nice code!
But search may be ending prematurely if string to search for is at the very end of the file.
Perhaps change the condition:
ulCurrentPosition < (ulFileSize - ulBufferSize)
to:
ulCurrentPosition < (ulFileSize - ulBufferSize 1)
I'm a physics major student. I need to write a program which searches a file and finds a couple of parameter then store them in another file. This code looks extremely helpful to me. Could you please send the full version to me.
Bests,
Hi all. I must write a program by C for Windows that search file in a folder as Windows. Please help me. Thanks all.
Hey man....I have been trying to write a program which examines each text file in a selected folder for lines containing a particular string.Any such lines are to be appended to a multi-line text box, preceded by their file name. If you could send me the code of which to implement this task to my gmail...Thankyou very much.
Related Tutorials
Related Source Code
C++ Job Search