M
Maxx
I'm writing a C program that will read data from a file and insert
them in a xml template.
for example the input file would contain
Class:::
Sniper
Country:::
Austria
Globaltime:::
1300250
Name:::
Niel;263
patrick;880
chris;344
And the output file would contain, the following:::
<item>
<Class>Sniper</Class>
<Country>Austria</Country>
<Name>Niel</Name>
<Logtime>263</Logtime>
<Globaltime>1300250</Globaltime>
</item>
<item>
<Class>Sniper</Class>
<Country>Austria</Country>
<Name>patrick</Name>
<Logtime>880</Logtime>
<Globaltime>1300513</Globaltime>
</item>
<item>
<Class>Sniper</Class>
<Country>Austria</Country>
<Name>chris</Name>
<Logtime>344</Logtime>
<Globaltime>1301393</Globaltime>
</item>
Now the program i've written is getting compiled well. But when i try
to run it, i'm getting an error which unhandled win32 exception. I've
tried numerous methods to find the source of problem but didn't
succeed .
Here's the program i've written:::
001 #include<stdio.h>
002 #include<string.h>
003 #include<stdlib.h>
004 #define MAX_LEN 1000
005
006
007 char *xml[MAX_LEN]={
008 "<item>",
009 " <Class></Class>",
010 " <Country></Country>",
011 " <Name></Name>",
012 " <Logtime></Logtime>",
013 " <Globaltime></Globaltime>",
014 "</item>"
015 };
016
017
018 char class[MAX_LEN],country[MAX_LEN],*name_list[MAX_LEN],*name;
019 int logtime[MAX_LEN],*lg,no_of_player=0;
020 static int globaltime=0,flag=5;
021 FILE *input,*output;
022 void parsing_line(char *buf);
023 void write_xml(int tim);
024 void write_file(char **buff);
025 void insert_xml(char *line,char *replace);
026 int convert_to_sec(int value);
027
028 int main(int argc,char *argv[])
029 {
030
031 int times=(*argv[2])-'0';
032 char line[MAX_LEN],name_out[20];
033 sprintf(name_out,"%s.readytohit",argv[1]); /*
name of output file */
034 if(argc<1)
035 {
036 fprintf(stderr,"Too few arguments");
037 exit(1);
038 }
039 if((input=fopen(argv[1],"r"))==NULL)
040 {
041 fprintf(stderr,"Can't open file %s",argv[1]);
042 exit(2);
043 }
044 if((output=fopen(name,"w"))==NULL)
045 {
046 fprintf(stderr,"Can't open file %s",name);
047 exit(2);
048 }
049 LOOP: while(fgets(line,MAX_LEN,input)!=NULL)
050 {
051
052
053 if(strcmp(line,"Class:::")==0)
{
054 fgets(line,MAX_LEN,input);
055 strcpy(class,line); /*
copy line to class */
056 goto LOOP;
057 } else if(strcmp(line,"Country:::")==0) {
058 fgets(line,MAX_LEN,input);
059 strcpy(country,line); /
*copy line to country */
060 goto LOOP;
061 }else if(strcmp(line,"Globaltime:::")==0){
062 fgets(line,MAX_LEN,input);
063 globaltime=atoline; /*
store the globaltime */
064 goto LOOP;
065 }else if(strcmp(line,"Name:::")==0){
066 goto LOOP;
067 }
068
parsing_line(line);
069 }
070
071 write_xml(times);
072 fclose(input);
073 fclose(output);
074 return 0;
075 }
076
077 void parsing_line(char *buf) /
* This function segregates the line into tokens */
078 {
079
080 name=strtok(buf,";" );
081 *lg=convert_to_sec((int)(strtok(buf,";")));
082 name++,lg++,no_of_player++;
083 }
084
085 void write_xml(int tim) /
*this function prepares the xml */
086 {
087 int i,j;
088 for(i=0;i<tim;i++)
089 {
090 name=*name_list;
091 lg=logtime;
092 for(j=0;j<no_of_player;j++,name++,lg++)
093 {
094 insert_xml(xml[1],class);
095 insert_xml(xml[2],country);
096 insert_xml(xml[3],name);
097 insert_xml(xml[4],(char *)lg);
098 insert_xml(xml[5],(char *)globaltime);
099 globaltime+=(*lg);
100 }
101 write_file(xml);
102 }
103 }
104
105 void insert_xml(char *line,char *replace) /*this function
insert the values in between the xml fields */
106 {
107 char *w,*linept,temparr[MAX_LEN];
108 linept=line,w=temparr;
109 while(*linept++!=replace[0])
110 {
111 *w++=*linept;
112 }*w='\0';
113 while(*replace!='\0')
114 {
115 *linept=*replace;
116 linept++,replace++;
117 }*linept='\0';
118 strcat(line,w);
119 }
120
121 void write_file(char **buff) /* this function writes the
final xml to file */
122 {
123 int i;
124 for(i=0;i<18;i++)
125 {
126
127 fputs(buff,output);
128 }
129 }
130
131 int convert_to_sec(int value)
132 {
133 int m;
134 m=value/100;
135 m=m*60+(value/100);
136 return m;
137 }
I've tried debugging the program by inserting printf into certain
places and so far found out that the condition part which the strcmp
function are not getting satisfied. But the parameters passed to
strcmp are equal.. And still i get an unhandled win32 exception
Please help me through this.
them in a xml template.
for example the input file would contain
Class:::
Sniper
Country:::
Austria
Globaltime:::
1300250
Name:::
Niel;263
patrick;880
chris;344
And the output file would contain, the following:::
<item>
<Class>Sniper</Class>
<Country>Austria</Country>
<Name>Niel</Name>
<Logtime>263</Logtime>
<Globaltime>1300250</Globaltime>
</item>
<item>
<Class>Sniper</Class>
<Country>Austria</Country>
<Name>patrick</Name>
<Logtime>880</Logtime>
<Globaltime>1300513</Globaltime>
</item>
<item>
<Class>Sniper</Class>
<Country>Austria</Country>
<Name>chris</Name>
<Logtime>344</Logtime>
<Globaltime>1301393</Globaltime>
</item>
Now the program i've written is getting compiled well. But when i try
to run it, i'm getting an error which unhandled win32 exception. I've
tried numerous methods to find the source of problem but didn't
succeed .
Here's the program i've written:::
001 #include<stdio.h>
002 #include<string.h>
003 #include<stdlib.h>
004 #define MAX_LEN 1000
005
006
007 char *xml[MAX_LEN]={
008 "<item>",
009 " <Class></Class>",
010 " <Country></Country>",
011 " <Name></Name>",
012 " <Logtime></Logtime>",
013 " <Globaltime></Globaltime>",
014 "</item>"
015 };
016
017
018 char class[MAX_LEN],country[MAX_LEN],*name_list[MAX_LEN],*name;
019 int logtime[MAX_LEN],*lg,no_of_player=0;
020 static int globaltime=0,flag=5;
021 FILE *input,*output;
022 void parsing_line(char *buf);
023 void write_xml(int tim);
024 void write_file(char **buff);
025 void insert_xml(char *line,char *replace);
026 int convert_to_sec(int value);
027
028 int main(int argc,char *argv[])
029 {
030
031 int times=(*argv[2])-'0';
032 char line[MAX_LEN],name_out[20];
033 sprintf(name_out,"%s.readytohit",argv[1]); /*
name of output file */
034 if(argc<1)
035 {
036 fprintf(stderr,"Too few arguments");
037 exit(1);
038 }
039 if((input=fopen(argv[1],"r"))==NULL)
040 {
041 fprintf(stderr,"Can't open file %s",argv[1]);
042 exit(2);
043 }
044 if((output=fopen(name,"w"))==NULL)
045 {
046 fprintf(stderr,"Can't open file %s",name);
047 exit(2);
048 }
049 LOOP: while(fgets(line,MAX_LEN,input)!=NULL)
050 {
051
052
053 if(strcmp(line,"Class:::")==0)
{
054 fgets(line,MAX_LEN,input);
055 strcpy(class,line); /*
copy line to class */
056 goto LOOP;
057 } else if(strcmp(line,"Country:::")==0) {
058 fgets(line,MAX_LEN,input);
059 strcpy(country,line); /
*copy line to country */
060 goto LOOP;
061 }else if(strcmp(line,"Globaltime:::")==0){
062 fgets(line,MAX_LEN,input);
063 globaltime=atoline; /*
store the globaltime */
064 goto LOOP;
065 }else if(strcmp(line,"Name:::")==0){
066 goto LOOP;
067 }
068
parsing_line(line);
069 }
070
071 write_xml(times);
072 fclose(input);
073 fclose(output);
074 return 0;
075 }
076
077 void parsing_line(char *buf) /
* This function segregates the line into tokens */
078 {
079
080 name=strtok(buf,";" );
081 *lg=convert_to_sec((int)(strtok(buf,";")));
082 name++,lg++,no_of_player++;
083 }
084
085 void write_xml(int tim) /
*this function prepares the xml */
086 {
087 int i,j;
088 for(i=0;i<tim;i++)
089 {
090 name=*name_list;
091 lg=logtime;
092 for(j=0;j<no_of_player;j++,name++,lg++)
093 {
094 insert_xml(xml[1],class);
095 insert_xml(xml[2],country);
096 insert_xml(xml[3],name);
097 insert_xml(xml[4],(char *)lg);
098 insert_xml(xml[5],(char *)globaltime);
099 globaltime+=(*lg);
100 }
101 write_file(xml);
102 }
103 }
104
105 void insert_xml(char *line,char *replace) /*this function
insert the values in between the xml fields */
106 {
107 char *w,*linept,temparr[MAX_LEN];
108 linept=line,w=temparr;
109 while(*linept++!=replace[0])
110 {
111 *w++=*linept;
112 }*w='\0';
113 while(*replace!='\0')
114 {
115 *linept=*replace;
116 linept++,replace++;
117 }*linept='\0';
118 strcat(line,w);
119 }
120
121 void write_file(char **buff) /* this function writes the
final xml to file */
122 {
123 int i;
124 for(i=0;i<18;i++)
125 {
126
127 fputs(buff,output);
128 }
129 }
130
131 int convert_to_sec(int value)
132 {
133 int m;
134 m=value/100;
135 m=m*60+(value/100);
136 return m;
137 }
I've tried debugging the program by inserting printf into certain
places and so far found out that the condition part which the strcmp
function are not getting satisfied. But the parameters passed to
strcmp are equal.. And still i get an unhandled win32 exception
Please help me through this.