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!

Search a File

Status
Not open for further replies.

logic4fun1

Programmer
Jun 29, 2006
6
US
All,
What i am trying to accomplish is to search for a particular STRING in a file of 10 million records. I am sure that there is only One Match available in that file and i dont want to read each line and compare with STRSTR.

I am just looking for options to accomplish this without reading each line. And this needs to be done in C.

Thanks in advance.
Logc4Fun
 
> I am just looking for options to accomplish this without reading each line.
What other choice is there?

Unless you know something else about the file, how can you skip lines without wondering whether that was the line it was on?

Maybe read the whole file into memory with a single fread() call?
10M records suggests at least a 100MB file, so the OS is going to start swapping out the beginning of the file before you've even finished reading it.

How about you explain what is so bad about
Code:
while ( fgets( buff, sizeof buff, fp ) != NULL ) {
  if ( strstr( buff, "needle" ) != NULL ) {
    printf("Found it\n");
    break;
  }
}

Yes there are faster string matchers (say Boyer Moore), but that is something you'd have to find, or code yourself.

--
 
Line oriented searches are going to be best accomplished
using stdio friendly functions as Salem suggested except
in very special cases.

Otherwise if all you need is an offset you could read
blocks of data using fread or read and record an offset counter. Might be faster, who knows.

Recently did something like this in a directory search
using mmap + thread pool and regexps. Good exercise but
a lot of work.
 
If you need to do this repeatedly (for instance it's a telephone directory) consider storing a copy of your file in a more useful (sorted?) order. Then you can achieve huge speed increases.
 
Agreed - details of the application are necessary for better advice.


--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top