Thread: Searching A Text File?

  1. #1
    Unregistered
    Guest

    Searching A Text File?

    Hello,

    How Do I Search A Text File For The String Which The User Entered?

    Thank You For Your Help

  2. #2
    looking for the truth moemen ahmed's Avatar
    Join Date
    Feb 2002
    Location
    Egypt
    Posts
    161
    one way is to open the file using function(fopen and fclose from stdio.h) and search for your word using (do ........while) loop using (getchar()) function till the end of file, but u ll ve to detect the spaces and put every word(it ll be alphas between two spaces) in a temp variable, and check to c if its ur word.


    good luck
    Programming is a high logical enjoyable art for both programer and user !!

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    79
    Here is an idea. A function can be created to search for a string within a string. If all the data in the text file is stored in a string array, then you're all set.

    Here is the function...

    #include<string.h>
    #define yes 1
    #define no 0
    #define max 256 // for example
    void nullify(char *string)
    {
    for(int i=0; i<max; i++)
    string[i]=0;
    }

    bool instr(char *a,char *b)
    {
    bool value=no;
    int *m=new int;
    char *temp=new char[max];
    for(int i=0; i<strlen(b); i++)
    {
    *m=0; nullify(temp);
    for(int l=i; l<strlen(b); l++)
    {
    temp[*m]=b[l];
    (*m)++;
    if(stricmp(a,temp)==0)
    {
    value=yes;
    break;
    }
    }
    }
    delete m;
    delete temp;
    return(value);
    }

    The instr function will return 1 if a is present in b, and 0 if it is not.
    Compiler:Borland C++ 5.0
    Last edited by sundeeptuteja; 07-03-2002 at 05:04 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Searching a text file for double quotation marks "
    By willie in forum C Programming
    Replies: 4
    Last Post: 11-23-2008, 02:00 PM
  2. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  3. Read word from text file (It is an essay)
    By forfor in forum C Programming
    Replies: 7
    Last Post: 05-08-2003, 11:45 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM