Solved

Fairly Simple Search Engine question

Posted on 1997-06-02
3
144 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

733 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