Thread: Read and compare file contain array of string

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    3

    Read and compare file contain array of string

    Hi all,

    I'm using C to read file contain array of string. The file contain this array:

    Code:
    {"Robin Van Persie","Alan Shearer","Bacary Sagna","Juergen Klinsmann"}
    I want to compare those array of string with char array. Here's what I've done so far:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main(void)
    {
      FILE *fp;
      char name[] = "Alan Shearer";
      int i;
      char *content[4];
      
      /* read the values */
      if((fp=fopen("test.txt", "r"))==NULL) {
        printf("Cannot open file.\n");
      }
    
    
      fgets(*content,sizeof(*content),fp);
      for(i=0;i<sizeof(content);i++) {
    	if(strcmp(content[i],name)==0) {
    	    printf("%s found",name);
    	}
      }   
      
      return 0;
    }
    when I run compiled program it shows error message:
    2 [main] myfile 2108 exception::handle: Exception: STATUS_ACCESS_VIOLATION 1182 [main] myfile 2108 open_stackdumpfile: Dumping stact trace to myfile.exe.stackdump

    Please help me, I'm new bie in C programming..

    Regards,
    Didin

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your content[] array is way too small. Try using content[100] (in a single dimension array), which is better suited for a row of text than a *content[], anyway.
    Code:
    while(fgets(content, sizeof(content), fp)) {
       if(strstr(content,name)) {
          printf("%s found",name);
          content[0]='\0'; 
       }
    }
    without any asterisks, is what you want.

  3. #3
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    3
    Quote Originally Posted by Adak View Post
    Your content[] array is way too small. Try using content[100] (in a single dimension array), which is better suited for a row of text than a *content[], anyway.
    Code:
    while(fgets(content, sizeof(content), fp)) {
       if(strstr(content,name)) {
          printf("%s found",name);
          content[0]='\0'; 
       }
    }
    without any asterisks, is what you want.
    It works, but what I want it's compare words not char. With those code if char name[]="Alan" it will matched with "Alan Shearer", I don't want like that, it mush match full words char name[]="Alan Shearer", less than it not found shows.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    strstr does partial string matches, so of course strstr will find a match. Consider if you do the following on your input line:

    Code:
    strstr("{\"Robin Van Persie\",\"Alan Shearer\",\"Bacary Sagna\",\"Juergen Klinsmann\"}\n", "Alan")
    This will find Alan in the string.

    A better approach might be to save each item in its own string and do a series of comparisons:
    Code:
    strcmp("Robin Van Persie", "Alan")
    strcmp("Alan Shearer", "Alan")
    strcmp("Juerguen Klinsmann"), "Alan")
    ...
    If strcmp ever gives you a 0, then you have found the string "Alan" in your set. Notice that you should save the part in between the double-quotes in your input each into its own string.

    If the number of strings to compare is small, then this approach is efficient. However, if the number of strings is allowed to grow arbitrarily, the approach is poor because for n strings you will need to make n comparisons in the worst case. A typical alternative here is to use a hash table if a large number of "lookups" will be performed.

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    3
    Quote Originally Posted by c99tutorial View Post
    strstr does partial string matches, so of course strstr will find a match. Consider if you do the following on your input line:

    Code:
    strstr("{\"Robin Van Persie\",\"Alan Shearer\",\"Bacary Sagna\",\"Juergen Klinsmann\"}\n", "Alan")
    This will find Alan in the string.

    A better approach might be to save each item in its own string and do a series of comparisons:
    Code:
    strcmp("Robin Van Persie", "Alan")
    strcmp("Alan Shearer", "Alan")
    strcmp("Juerguen Klinsmann"), "Alan")
    ...
    If strcmp ever gives you a 0, then you have found the string "Alan" in your set. Notice that you should save the part in between the double-quotes in your input each into its own string.

    If the number of strings to compare is small, then this approach is efficient. However, if the number of strings is allowed to grow arbitrarily, the approach is poor because for n strings you will need to make n comparisons in the worst case. A typical alternative here is to use a hash table if a large number of "lookups" will be performed.
    Thanks, but how to extract the file so I get each value for compare.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by didinj View Post
    Thanks, but how to extract the file so I get each value for compare.
    If the parts in-between your double-quotes never contain the characters DELIM, where DELIM is the set of your delimiting characters
    Code:
    const char DELIM[] = "{},\"\n" ;
    then a simple way is to use strtok. Here is an example for this type of input

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    static char line[100000];
    static const char DELIM[] = "{},\"\n";
    
    int main()
    {
        char *s, *w;
        while (fgets(line, 100000, stdin) != NULL) {
            s = line;
            while ((w = strtok(s, DELIM)) != NULL) {
                s = NULL;
                printf("Found a word w: \"%s\"\n", w);
            }
        }
        return 0;
    }
    If you give it the input from above, the output will be the four strings

    Robin Van Persie
    Alan Shearer
    Bacary Sagna
    Juergen Klinsmann

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 12-07-2012, 10:44 PM
  2. Replies: 8
    Last Post: 12-08-2009, 12:55 PM
  3. Replies: 1
    Last Post: 03-19-2009, 10:56 AM
  4. Replies: 16
    Last Post: 01-04-2007, 03:38 PM
  5. Replies: 1
    Last Post: 12-01-2002, 01:24 PM