0

Hi, as newbie in c, i have some problems with my code.
I don't know how to read data from into array,
and second, how i can compare data in array?

#include <stdio.h>
#include <conio.h>
#include <string.h>

int main (void){

 int i;
 char line [240], *pos;
 char name[1000];
 FILE *fp;
 char myfile[20]="names.xml";
     fp=fopen(myfile, "r");
         if (!fp){printf("Can't open file..\n", myfile);
         getch(); return 1; }

 while (!feof(fp)){
  fgets(line, sizeof(line), fp);
     pos = strtok (line, "<>");
       if (strcmp(pos, "Name") == 0){
               for (i=0;i<3;i++){
                     pos = strtok(NULL,"<>");
                         if ((pos != NULL)&&(i==0)){
                            sscanf(pos, &name[i]); //can't read into array
                            printf("%s\n", pos);  // for pos test
                            }
                 }
        }
 }
 fclose(fp);
 //for testing array
// for (i=0;i<6;i++) //without this line gives <null>
    {printf("\n%s", name[i]);}

 printf("\nPress any key..");
 getch();
 return 0;
}

Thanks for any help.

Attachments
<xml>
<Names>
<Name> Timmu </Name><Age> 15 </Age>
<Name> Dan </Name><Age> 40 </Age>
<Name> Jake </Name><Age> 5 </Age>
<Name> Timmu </Name><Age> 20 </Age>
<Name> Dan </Name><Age> 45 </Age>
<Name> Timmu </Name><Age> 20 </Age>
</Names>
</xml>
2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by iamthwee
0

1. c/c++ isn't the best choice of language to read 'n' parse xml.
2. You might need to contruct a tree of some sort to retain the importance of the xml hierarchy -- or use a third party library. I believe the defacto is expat?
3. Your program has elementary bugs that need fixing first.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.