Like Tree2Likes

Search string in a text file

This is a discussion on Search string in a text file within the C Programming forums, part of the General Programming Boards category; Hey everyone! I am having trouble with searching. My code compiles but doesn't give what I want. When I output ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Hong Kong
    Posts
    18

    Search string in a text file

    Hey everyone! I am having trouble with searching. My code compiles but doesn't give what I want. When I output the database array it only gives me the last word of the input file repeated d_size many times...Here is the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    const unsigned C_MAX = 1000;
    
    int exist(char* database[], unsigned dz, char* target){
      unsigned lower = 0;
      unsigned upper = dz;
      while (lower < upper){
        unsigned middle = (lower + upper)/2;
        if(strcmp(database[middle],target) == 0)
          return 1;
        if(strcmp(database[middle],target) > 0)
          upper = middle;
        else
          lower = middle + 1;
    }
       return 0;
    }
    
    void swap(char **w, char **a)
    {
         char *temp;
         temp = *w;
         *w = *a;
         *a = temp;
    }
    
    void sort(char **word, unsigned int length)
    {
        int i = 0;
        int j = 0;
        for(i=0; i<length; i++)
        for(j=i+1; j<length; j++)
          if(strcmp(word[i],word[j])>0){
            swap(&word[i],&word[j]);
        }
    }
    
    int main(int argc, char ** argv){
      FILE *file;
      file = fopen(argv[1],"r");
      
      unsigned d_size = 0;
      char* database[C_MAX];
      char w[C_MAX];
      while (d_size < C_MAX && fscanf(file,"%s",w)!=EOF){
        char *p = malloc(strlen(w)+1);
        strcpy(p,w);
        database[d_size] = p;
        ++d_size;
      }
    
      sort(database, 0 + d_size);
    
      while (scanf("%s",w)){
        if (exist(database, d_size, w) == 1)
          printf("yes\n");
        else
          printf("no\n");
      }
       return 0;
    }
    Last edited by stud91; 09-24-2011 at 10:56 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's a miracle it runs at all. You have pointers all over the place, and no memory allocated for them.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Hong Kong
    Posts
    18
    sorry my bad....i posted the old version...now i have updated...the code does compile in the new version and not in the old one.....

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    > char *w;
    > char *w[C_MAX];
    > char w[C_MAX];
    OK, in 3 edits, you've managed to arrive at a decent declaration of w.

    What you now need to do is make a COPY of each string, before you do this.
    database[d_size] = w;

    Because all this does is make pointers to the SAME string.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You are making all of your pointers point at w. You need to allocate space for every pointer, then copy each word to each correct pointer as you go along.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Location
    Hong Kong
    Posts
    18
    can you tell me how to do that coz im new at C...

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    Without error checking,
    char *p = malloc( strlen(w)+1 );
    strcpy(p,w);
    array[pos] = p;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Sep 2011
    Location
    Hong Kong
    Posts
    18
    Thanks for reply! I have edited the code but it still doesn't work.....As in if the input text file contains:
    "This program is in C."
    And i search for "This" it gives me a no. Is there any problem with my exist function...

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by stud91 View Post
    Thanks for reply! I have edited the code but it still doesn't work.....As in if the input text file contains:
    "This program is in C."
    And i search for "This" it gives me a no. Is there any problem with my exist function...
    1) First lesson in programming ... "Compiles" does not mean "Works"...
    2) You say you've revised your code... how about posting the new version for us...

    The original exist function has problems...
    Code:
    int exist(char* database[], unsigned dz, char* target){
      unsigned lower = 0;
      unsigned upper = dz;
      while (lower < upper){
        unsigned middle = (lower + upper)/2;
        if (database[middle] == target)
          return 1;
        if (database[middle] > target)
          upper = middle;
        else
          lower = middle + 1;
      }
      return 0;
    }
    C does not know how to compare arrays (and strings are just char arrays) across == or with >... what you are actually doing is comparing the first character at database middle to the first character of your target string... you need to use strcmp(). So look that up in your C library documentation.

    Oddly enough you appeared to know how to do this in your search function...

  10. #10
    Registered User
    Join Date
    Sep 2011
    Location
    Hong Kong
    Posts
    18
    Thanks guys...Code updated and it executes the way it should. @CommonTater i know...just being careless ...Actually this is my first C course...I have C++ & Java background though....
    Thanks again, you guys are awesome...

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by stud91 View Post
    Last edited by stud91; 6 Minutes Ago at 09:56 AM.
    Please, stop editing the original post with your code. It makes the thread extremely hard to follow and also serves to render previous people's posts obfuscated. Post your changed code as a new comment with your current problem.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    Please, stop editing the original post with your code. It makes the thread extremely hard to follow and also serves to render previous people's posts obfuscated. Post your changed code as a new comment with your current problem.
    In a past conversation Webmaster indicated that it might be possible to block editing of first messages but was reluctant to do so unless it became a problem.

    This seems to be epidemic... 3 times in 2 days... it reduces the rest of the conversation to the level of nonsense: "This code looks fine so what are they on about?"... not good.

    I wonder if this might be the time to actually request the feature...
    manasij7479 likes this.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by stud91 View Post
    Thanks guys...Code updated and it executes the way it should.
    You're welcome for the help ... but, as you can see, we're not too happy about you making the rest of the conversation appear nonsensical.

  14. #14
    Registered User
    Join Date
    Sep 2011
    Location
    Hong Kong
    Posts
    18
    I am really sorry about this. Actually this is my first posting in such kind of forums.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by stud91 View Post
    I am really sorry about this. Actually this is my first posting in such kind of forums.
    Thank you... for that.

    You shouldn't feel too bad... You're kind of the straw that broke the camel's back on this issue... it's been around for a while.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String search in two text files
    By ab-c in forum C Programming
    Replies: 2
    Last Post: 07-14-2008, 04:01 AM
  2. search text from file
    By bradleyd in forum C++ Programming
    Replies: 11
    Last Post: 07-07-2008, 10:54 AM
  3. Search text in file
    By MarlonDean in forum C++ Programming
    Replies: 10
    Last Post: 05-16-2008, 03:51 AM
  4. search for text string in a file
    By basenews in forum C++ Programming
    Replies: 2
    Last Post: 05-03-2007, 05:15 AM
  5. string array text search
    By ery in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2001, 02:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21