Thread: problem with comparing tokenised strings with array elements

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    4

    Question problem with comparing tokenised strings with array elements

    Code:
    #include <stdio.h> // standard IO
    #include <conio.h> // keyboard-monitor 
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    int main(void) 
    { 
        int n, i; // variables declaring
        int x = 0;
        char *old_EU[] = {"Austria", "Belgium", "Spain", "Holland", "Ireland", "Italy", "Greece", "Luxembrug", "Portugal", 
        "France", "Sweden", "Germany", "Finnish", "Great Britain", "Denmark"};
        n=sizeof(old_EU)/sizeof(int); // get the number of different countries in array
        char *file="c:\\Import.xml";
        char rida[240], *pos;
        FILE *fp;
       	fp=fopen(file, "r");
    	if(fp){ // file opened successfully
    	   while (!feof(fp)){
    	       fgets(rida, sizeof(rida), fp);
    	       pos=strtok(rida, "<>");
    	       do{
                  for (i=0; i<n; i++) {
                  if (strcmp(old_EU[i], pos) == 0)                      
                  x++;
                  //printf("%d", x);
                  }
                  printf("%s\n", pos);
                  pos=strtok(NULL, "<>");
               }while(pos && *pos!='\n');
    	   }
        }  
        else printf
    	("File %s couldnt be opened\n",
    		file);
    	printf("\n%d , % d", x, n);
        printf("\nPress any key...");
        getch(); 
        return 0;
    }
    It prints all the tokenised strings on separate lines and im assuming that they are stored in pos, but the strcmp never finds a same string ? what is the problem here?

    And here is a small cut from my xml file to be clearer
    <Import partner>
    <Country name>Argentina </Country name><Import quantity (kr)> 5670753 </IImport quantity (kr)>
    </Import partner>
    <Import partner>
    <Country name>Aruba </Country name><Import quantity (kr)> 0 </Import quantity (kr)>
    <Import partner>
    <Import partner>
    <Country name>Australia </Country name><Import quantity (kr)> 1263834 </Import quantity (kr)>
    <Import partner>
    <Import partner>
    <Country name>Austria </Country name><Import quantity (kr)> 129511215 </Import quantity (kr)>
    </Import partner>

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    your filds have spaces inside - use Trim (samples of such function were posted not long ago) to get rid of them

    also - read FAQ about using feof to control loops (you should not)

    Code:
    while ( fgets(rida, sizeof(rida), fp))
    should be used
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
        n=sizeof(old_EU)/sizeof(int); // get the number of different countries in array
    Warning, Warning - sizeof(int) != sizoef(char*) in all circumstances, so your n may be half of what you expect [for example].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    (sorry if I duplicate anything mentioned above)

    Code:
    #include <stdio.h> // standard IO
    #include <conio.h> // keyboard-monitor  note that conio.h is deprecated in C99 IIRC
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    int main(void) 
    { 
        int n, i; // variables declaring
        int x = 0;
        char *old_EU[] = {"Austria", "Belgium", "Spain", "Holland", "Ireland", "Italy", "Greece", "Luxembrug", "Portugal", 
        "France", "Sweden", "Germany", "Finnish", "Great Britain", "Denmark"};
        n=sizeof(old_EU)/sizeof(int); // get the number of different countries in array Bad idea. As matsp points out, this won't give the result you're looking for.
        char *file="c:\\Import.xml"; // you can use forward slashes when using the C standard library
        char rida[240], *pos;// why 240? where does this magic number come from?
        FILE *fp;
       	fp=fopen(file, "r"); // these two lines can be changed to if((fp = fopen(file, "r")) != NULL)
    	if(fp){ // file opened successfully
    	   while (!feof(fp)){ // as mentioned before, using feof to control a loop is a pretty bad idea. replace this and the next line with while(fgets(rida, sizeof(rida), fp) != EOF)
    	       fgets(rida, sizeof(rida), fp); // duplicated below! there's a way to avoid this . . . there's a reason that you have a do . . . while loop down there . . . even if you are using this to get past the first '<' in the xml file, it's still a bad idea to duplicate code like this. think on it, there *is* a better way.
    	       pos=strtok(rida, "<>");
    	       do{
                  for (i=0; i<n; i++) {
                  if (strcmp(old_EU[i], pos) == 0)                      
                  x++;
                  //printf("%d", x);
                  }
                  printf("%s\n", pos);
                  pos=strtok(NULL, "<>");
               }while(pos && *pos!='\n');
    	   }
        }  
        else printf
    	("File %s couldnt be opened\n",
    		file);
    	printf("\n%d , % d", x, n); // why x?
        printf("\nPress any key...");
        getch();  [color=red]// use getchar(), not getch() . . .
        return 0;
    }
    Hope this helps a little.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    4

    other problems

    thanks alot for your help, i can't edit my first post and thread-title so i need make a new post now.
    thanks for the help, didn't notice that the problem is in the space. it finds the array elements from the file now. what i wanted to do is to sum all these countries import numbers which are found from my array. got some pseudocode in my mind... so how to to jump 2 lines ahead where the number is(for which functions/commands should i search the web?)? and then i assume to convert it with atoi to integer

    didnt know that C doesnt have trim function, i searched for some on the web, but they seem to be complicated and not explained, ill leave it for next step.

    Code:
    int main(void) 
    { 
        int n, i; // variables declaring
        int x = 0;
        char *old_EU[] = {"Austria ", "Belgium ", "Spain ", "Holland ", "Ireland ", "Italy ", "Greece ", "Luxembrug ", "Portugal ", 
        "France ", "Sweden ", "Germany ", "Finnish ", "Great Britain ", "Denmark "};
        n=sizeof(old_EU)/sizeof(int); /* get the number of different countries in array
     Bad idea. This won't give the result you're looking for. 
    but it works now for me with these small arrays, i havent seen other ways to count em */
        char *file="c://Import.xml";
        char rida[240], *pos;
        FILE *fp;
       	fp=fopen(file, "r"); /* these two lines can be changed to if((fp = fopen(file, "r")) != NULL) 
    it brings me message then that expected unqualified-id before "else" and ; expected */
    	if(fp){ // file opened successfully
    	   while (fgets(rida, sizeof(rida), fp) != NULL) { // explanation why -  http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1046476070&id=1043284351
               pos=strtok(rida, "<>");
    	       do{
                  for (i=0; i<n; i++) {
                  if (strcmp(old_EU[i], pos) == 0)                      
                  x++;
                  //printf("%d", x);
                  }
                  printf("%s\n", pos);
                  pos=strtok(NULL, "<>");
               }while(pos && *pos!='\n');
    	   }
        }  
        else {
             printf("File %s couldnt be opened\n", file);
             }
    	printf("\n%d, % d", x, n); /* why x? 
    just to see how many elements it found */
        printf("\nPress any key...");
        getch();  /* use getchar(), not getch() . . . 
    well, with getchar it closes only when i press "enter" but with getch it closes with any key pressed as written on the screen printf("\nPress any key..."); */
    
        return 0;
    }
    Last edited by sky; 03-18-2009 at 06:24 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with array of strings
    By cole in forum C++ Programming
    Replies: 11
    Last Post: 09-28-2008, 11:45 PM
  2. using realloc for a dynamically growing array
    By broli86 in forum C Programming
    Replies: 10
    Last Post: 06-27-2008, 05:37 AM
  3. Problem with assigning value to array elements
    By sagitt13 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2004, 11:26 AM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM
  5. ? comparing elements of a char array
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 09-20-2001, 07:55 AM