Ben Bacarisse
4/2/2011 10:17:00 AM
Maxx <grungeddd.maxx@gmail.com> writes:
<snip>
> 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)
This does not do what you think it does. argc < 1 is a very odd
condition. When there is one command line argument argc is 2 because
argv[0] is the program name and argv[1] is the argument (and argv[2] is
NULL).
However, it's too late to test argc since you have already used argv[1]
and argv[2].
> 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)
name is NULL. You probably meant name_out.
> 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)
This will never be true because fgets keeps the newline (if it can).
> {
> 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;
There is no such variable. This is not the code you've been trying to
debug!
> /* store the globaltime */
> 064 goto LOOP;
> 065 }else if(strcmp(line,"Name:::")==0){
> 066 goto LOOP;
> 067 }
> 068
> parsing_line(line);
There is no need for all these gotos. C has a continue statement.
> 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,";")));
The cast (the "(int)" part) does not do what you think it does. You
need atoi or strtol to convert a string to an integer.
You also need to read up on how to use strtok.
> 082 name++,lg++,no_of_player++;
The code will be easier to follow with fewer global variables.
> 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 }
This looks all wrong. What are the loops there for?
> 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 }
If you add some more space, I might look at this!
> 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++)
18?
> 125 {
> 126
> 127 fputs(buff[i],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 }
That's a very strange calculation. It's the same as
return (60 * value + 1) / 100;
(I think). Can that be correct?
> 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..
Anything is possible because you did not post the program you are
running.
> And still i get an unhandled win32 exception
>
> Please help me through this.
Why are you using C for this? There are lots of languages designed
specifically for this sort of processing.
--
Ben.