Solved

Fairly Simple Search Engine question

Posted on 1997-06-02
3
142 Views
Last Modified: 2013-12-25
We have been using a REXX script for searching our email database in the past.  However we would like to move to CGI to do this.  The problems I am having is the type of file we  had was 100's of names lined up like this:

>Jones, Scott
Email: here@there.com
>Johnson, Becky
Email: there@here.com

The '>' was there so the REXX script would know where to start looking for the name that was entered by the user.  Is there anyway we could convert this to a perl or C script to do the same thing, Returning the persons name and the next line (their email)?  Thanks in advance!

Dan
0
Comment
Question by:Primetyme
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
julio011597 earned 140 total points
ID: 1828174
Hello Dan,

one simple option is to code a sort of grep function that scans the file until it finds the right name; something like this:

--//--
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/* search():
 * name: pointer to a string containing the name to look for
 * fp:   pointer to the file to scan (supposed already open for reading)
 *
 * returns a pointer to a string containing the e-mail address
 */

char *search(char *name, FILE *fp)
{
  char *buff;
  char str[BUFSIZ];

  if(!(buff = malloc(BUFSIZ))) /* malloc() failed! */
    return NULL;

  (void) rewind(fp) /* let's go to the BOF */

  sprintf(str, ">%s", name);

  while(fgets(buff, BUFSIZ, fp)) { /* get name line */
    if(strstr(buff, str)) /* found? */
      return fgets(buff, BUFSIZ, fp); /* return buff pointer */
    fgets(buff, BUFSIZ, fp); /* skeep e-mail line */
  }

  return NULL; /* name not found */
}
--//--

This should be enough for a hundred lines. If you still need faster performance, i'd suggest to build a simple C program to translate your database to a dbm (better ndbm) table.
I could give you some sample code for this too.

Rgds, julio
0
 

Author Comment

by:Primetyme
ID: 1828175
I would really appreciate some sort of code in C since that
is what I am more familiar with :)  and do i need to have the
.txt file with all these names in the same dir? (it is) Thanks
again :)

Dan
0
 
LVL 5

Expert Comment

by:julio011597
ID: 1828176
Well, that _was_ C code! :)

Really, are you asking for a complete working CGI code, going with the sequential method just shown?

If so, it's ok: do you prefer GET or POST method?

Don't worry about where to put the text file.

BTW, maybe this could be worth a bit more points.

Cheers, julio
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial will discuss the log-in process using WhizBase. In this article I assume you already know HTML. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you might look at some of my other articles abo…
It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now