I'm getting an unhandled win32 exception while running this program


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.
 
Ad

Advertisements

I

Ian Collins

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>

If you want people to test your code, don't post it with line numbers!
 
B

Ben Bacarisse

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,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.
 
K

Keith Thompson

Maxx said:
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> [snip]
</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> [...]
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.

As Ian Collins wrote, posting code with line numbers is not helpful.
If we're going to try it ourselves, we need to save your code to
a file and (try to) compile it, which means we have to remove the
line numbers.

Just this once, I took the time to do that. I also had to re-join
several lines that had been split, probably by your news software; in
many cases, opening comment delimiters "/*" were split across lines.

When you post code, omit the line numbers and reformat your code so
it's no wider than 80 columns, preferably about 72. And use spaces,
not tabs.

Once I cleaned that up, I got several errors and warnings from my
compiler. "flag" is declared but never used. (If your compiler
didn't warn you about that, fiddle with its command-line options
until it does.) You consistently ignore the value returned
by fgets() (probably ok initially, but you should think about
how to handle input errors). And worst of all, on the line
"globaltime=atoline;", "atoline" is undeclared, and I don't see
anything for which it might be a plausible typo.

If you're posting code here, always post the *exact* code that
you compiled. Copy-and-paste it, or insert the source file into
your news client's buffer, or whatever it takes.

Additional whitespace would make the code much easier to read; in
particular, I suggest adding a space after each comma, and spaces
around most operators. For example, rather than

int times=(*argv[2])-'0';
char line[MAX_LEN],name_out[20];
sprintf(name_out,"%s.readytohit",argv[1]);

I'd write:

int times = *argv[2] - '0'; /* note: parentheses aren't helpful */
char line[MAX_LEN], name_out[20];
sprintf(name_out, "%s.readytohit", argv[1]);

That first line, the initialization of times, assumes that argv[2]
exists. What happens if I run your program with no command-line
arguments?

Why on Earth do you use gotos in your while loop? Each "goto LOOP;"
just jumps to the top of the loop -- which is where control was
going to go anyway.

*lg=convert_to_sec((int)(strtok(buf,";")));

strtok() returns a char*. What do you expect converting that
pointer value to int to accomplish?

insert_xml(xml[4],(char *)lg);

lg is an int*. You convert it to a char*, and pass it to a function
that treats it as a pointer to a string. This cannot end well.

insert_xml(xml[5],(char *)globaltime);

globaltime is an int. Again, you convert it to a char*. Converting
an int to a char* doesn't do what you seem to think it does.

All casts should be viewed with suspicion. Sometimes they're
appropriate, or even necessary, but you should be able to do what you're
trying to do with using any casts at all.

I think you've bitten off more than you can chew here. Getting your
code to compile doesn't mean it will work, and it doesn't mean you
haven't made so many mistakes that it makes more sense to start over
again.

Start small, say, with a program that just reads one line of input,
parses it, and prints out some information. Get that *working* (not
just compiling), and then add the next bit of functionality. Never add
anything new until what you have so far works correctly. Iterate
until you have a working program. Feel free to post here if you have
questions along the way.

You might find that <http://www.c-faq.com/> is a valuable resource.
 
B

BartC

As Ian Collins wrote, posting code with line numbers is not helpful.
If we're going to try it ourselves, we need to save your code to
a file and (try to) compile it, which means we have to remove the
line numbers.

Just this once, I took the time to do that. I also had to re-join

How long did it take?

Just asking because even my ancient 1980s text editor only took about ten
seconds.

(And how did the line numbers get there anyway?)
 
K

Keith Thompson

BartC said:
How long did it take?

Just asking because even my ancient 1980s text editor only took about ten
seconds.

Not very long, though more than 10 seconds. I also had to deal with the
lines that had wrapped, so deleting the first three characters on each
line would have messed things up.
(And how did the line numbers get there anyway?)

No idea, but they were there in the original post. (Probably the OP was
trying to be helpful.)
 
Ad

Advertisements

W

Willem

Keith Thompson wrote:
) Not very long, though more than 10 seconds. I also had to deal with the
) lines that had wrapped, so deleting the first three characters on each
) line would have messed things up.

Joining all lines that don't start with three digits is quite easy in some
text editors, though not all. It would take more than 10 seconds, though,
to realise this and to figure out the exact command syntax.

) No idea, but they were there in the original post. (Probably the OP was
) trying to be helpful.)

If you post a compiler error message saying 'such and such on line 123',
*then* line numbers in the code are incredibly helpful.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
J

James Kuyper

Keith Thompson wrote: ....
) No idea, but they were there in the original post. (Probably the OP was
) trying to be helpful.)

If you post a compiler error message saying 'such and such on line 123',
*then* line numbers in the code are incredibly helpful.

A better approach is to put a comment on line 123, indicating that it is
the line which the compiler is talking about. That way it doesn't
interfere with cut-paste-compile.

In most cases that I've seen in this newsgroup, if the OP's compiler
complains about line 123, than so will mine, if I set the warning levels
high enough (I set them pretty high, by default). As a result, it's
seldom necessary to rely upon line numbers if the code is complete and
presented in a way that makes it easy to cut-and-paste into a program.

For some odd reason, convincing people to provide complete compilable
examples of the problem they're having is usually the hardest part of
the process. However, if they only provide incomplete code, not having
line numbers to match up with the error messages is usually the least of
the problems with trying to figure out what's wrong.
 
G

Geoff

how to handle input errors). And worst of all, on the line
"globaltime=atoline;", "atoline" is undeclared, and I don't see
anything for which it might be a plausible typo.

globaltime = atoi(line);

but he's all confused over char arrays, pointers to char and
conversions from strings to int.

His declarations and cramped spacing makes it all the harder to read
or debug. His excessive use of commas makes interpreting warnings and
error messages from the compiler harder to interpret correctly.
 
M

Maxx

If you want people to test your code, don't post it with line numbers!

I'm extremely sorry for that..Actually i was using a stupid file
copying application that copied the whole damn thing including the
line numnbers..
I will post a fresh one without the line numbers
 
M

Maxx

If you want people to test your code, don't post it with line numbers!

Here i've posted the program without line numbers::


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX_LEN 1000
char *xml[MAX_LEN]={
"<item>",
" <Class></Class>",
" <Country></Country>",
" <Name></Name>",
" <Logtime></Logtime>",
" <Globaltime></Globaltime>",
"</item>"
};

char class[MAX_LEN],country[MAX_LEN],*name_list[MAX_LEN],*name;
int logtime[MAX_LEN],*lg,no_of_player=0;
static int globaltime=0,flag=5;
FILE *input,*output;
void parsing_line(char *buf);
void write_xml(int tim);
void write_file(char **buff);
void insert_xml(char *line,char *replace);
int convert_to_sec(int value);

int main(int argc,char *argv[])
{

int times=(*argv[2])-'0';
char line[MAX_LEN],name_out[20];
sprintf(name_out,"%s.readytohit",argv[1]); /
*name of output file */

if(argc<1)
{
fprintf(stderr,"Too few arguments");
exit(1);
}
if((input=fopen(argv[1],"r"))==NULL)
{
fprintf(stderr,"Can't open file %s",argv[1]);
exit(2);
}
if((output=fopen(name,"w"))==NULL)
{
fprintf(stderr,"Can't open file %s",name);
exit(2);
}
LOOP: while(fgets(line,MAX_LEN,input)!=NULL)
{

if(strcmp(line,"Class:::")==0)
{
fgets(line,MAX_LEN,input);

strcpy(class,line); /* copy line to class
*/
goto LOOP;
} else if(strcmp(line,"Country:::")==0) {
fgets(line,MAX_LEN,input);

strcpy(country,line); /*copy line to country
*/
goto LOOP;
}else if(strcmp(line,"Globaltime:::")==0){
fgets(line,MAX_LEN,input);

globaltime=atoline; /*store the globaltime
*/
goto LOOP;
}else if(strcmp(line,"Name:::")==0){
goto LOOP;
}

parsing_line(line);
}

write_xml(times);
fclose(input);
fclose(output);
return 0;
}


void parsing_line(char *buf) /*
This function segregates the line into tokens */
{
name=strtok(buf,";" );
*lg=convert_to_sec((int)(strtok(buf,";")));
name++,lg++,no_of_player++;
}



void write_xml(int tim) /
*this function prepares the xml */
{
int i,j;
for(i=0;i<tim;i++)
{
name=*name_list;
lg=logtime;
for(j=0;j<no_of_player;j++,name++,lg++)
{
insert_xml(xml[1],class);
insert_xml(xml[2],country);
insert_xml(xml[3],name);
insert_xml(xml[4],(char *)lg);
insert_xml(xml[5],(char *)globaltime);
globaltime+=(*lg);
}
write_file(xml);
}
}



void insert_xml(char *line,char *replace) /*this function
insert the values in between the xml fields */
{
char *w,*linept,temparr[MAX_LEN];
linept=line,w=temparr;
while(*linept++!=replace[0])
{
*w++=*linept;
}*w='\0';
while(*replace!='\0')
{
*linept=*replace;
linept++,replace++;
}*linept='\0';
strcat(line,w);
}

void write_file(char **buff) /* this function writes the
final xml to file */
{
int i;
for(i=0;i<18;i++)
{

fputs(buff,output);
}
}

int convert_to_sec(int value)
{
int m;
m=value/100;
m=m*60+(value/100);
return m;
}
 
Ad

Advertisements

M

Maxx

<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.
Yeah my mistake it will be name_out
This will never be true because fgets keeps the newline (if it can).

Yeah i can't figure out this part at all.fgets is supposed to get a
fresh line everytime its called, so in here i wanted it to get a fresh
line each time and store it in line[] and do the parsing on the line..
There is no such variable.  This is not the code you've been trying to
debug!
Sorry it was meant to be globaltime=atoi(line)..
There is no need for all these gotos.  C has a continue statement.

Ok got it..
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.


The code will be easier to follow with fewer global variables.

yeah i intended the values returned by strtok to be casted to int. It
will be
*lg=convert_to_sec(atoi(strtok(buf,";")));
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?

The inner loop is to insert the values in all the xml fields,so it's
supposed to loop through the no_of_players provided and print the xml
template and the outer loop is the no of time the user wants to write
the xml template
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!

void insert_xml(char *line,char *replace) /* this function insert
values in xml fields */
{

char *w, *linept, temparr[MAX_LEN];
linept=line, w=temparr;

while(*linept++!=replace[0])
{

*w++=*linept;
}*w='\0';

while(*replace!='\0')
{

*linept=*replace;
linept++,replace++;
}*linept='\0';
strcat(line,w);
}
sorry it will be 7 which is the number of rows in the xml array..
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        }


That's a very strange calculation.  It's the same as

  return (60 * value + 1) / 100;

(I think).  Can that be correct?


Yeah yeah that was the code i was looking for (doing all the
calculation in a single return statement) but couldn't figure out how
to do it.
Anything is possible because you did not post the program you are
running.

Actually i was changing the original values when trying to debug the
program.. and ended up posting the debugged version here.. Got all
messed up
Why are you using C for this?  There are lots of languages designed
specifically for this sort of processing.

unfortunately i don't know anything else other than c and little of c+
+ and java
 
M

Maxx

Maxx said:
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> [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> [...]
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.

As Ian Collins wrote, posting code with line numbers is not helpful.
If we're going to try it ourselves, we need to save your code to
a file and (try to) compile it, which means we have to remove the
line numbers.

Just this once, I took the time to do that.  I also had to re-join
several lines that had been split, probably by your news software; in
many cases, opening comment delimiters "/*" were split across lines.

When you post code, omit the line numbers and reformat your code so
it's no wider than 80 columns, preferably about 72.  And use spaces,
not tabs.

Once I cleaned that up, I got several errors and warnings from my
compiler.  "flag" is declared but never used.  (If your compiler
didn't warn you about that, fiddle with its command-line options
until it does.)  You consistently ignore the value returned
by fgets() (probably ok initially, but you should think about
how to handle input errors).  And worst of all, on the line
"globaltime=atoline;", "atoline" is undeclared, and I don't see
anything for which it might be a plausible typo.

Sorry about that it will be globaltime=atoi(line);
If you're posting code here, always post the *exact* code that
you compiled.  Copy-and-paste it, or insert the source file into
your news client's buffer, or whatever it takes.

Additional whitespace would make the code much easier to read; in
particular, I suggest adding a space after each comma, and spaces
around most operators.  For example, rather than

    int times=(*argv[2])-'0';
    char line[MAX_LEN],name_out[20];
    sprintf(name_out,"%s.readytohit",argv[1]);

I'd write:

    int times = *argv[2] - '0'; /* note: parentheses aren't helpful*/
    char line[MAX_LEN], name_out[20];
    sprintf(name_out, "%s.readytohit", argv[1]);

That first line, the initialization of times, assumes that argv[2]
exists.  What happens if I run your program with no command-line
arguments?

Why on Earth do you use gotos in your while loop?  Each "goto LOOP;"
just jumps to the top of the loop -- which is where control was
going to go anyway.
.....................................................
I was confused between continue and goto... Always thought that
continue would take the control to the next test expression
.............................

    *lg=convert_to_sec((int)(strtok(buf,";")));

strtok() returns a char*.  What do you expect converting that
pointer value to int to accomplish?

........................................................................

the input file will contain lines like this "Niel;263", so strtok
would separate the string into tokens delimited by ";".. Thus the
first part would go in name and the second part in logtime.
it would be actually
*lg=convert_to_sec(atoi((strtok(buf,";"))));
..............................................................................

    insert_xml(xml[4],(char *)lg);

lg is an int*.  You convert it to a char*, and pass it to a function
that treats it as a pointer to a string.  This cannot end well.

    insert_xml(xml[5],(char *)globaltime);

globaltime is an int.  Again, you convert it to a char*.  Converting
an int to a char* doesn't do what you seem to think it does.

All casts should be viewed with suspicion.  Sometimes they're
appropriate, or even necessary, but you should be able to do what you're
trying to do with using any casts at all.

............................................................................
I made the casts so that the integer values would get converted into
string and then pass it to the function.. Again all messed up.
...........................................................................


I think you've bitten off more than you can chew here.  Getting your
code to compile doesn't mean it will work, and it doesn't mean you
haven't made so many mistakes that it makes more sense to start over
again.

Start small, say, with a program that just reads one line of input,
parses it, and prints out some information.  Get that *working* (not
just compiling), and then add the next bit of functionality.  Never add
anything new until what you have so far works correctly.  Iterate
until you have a working program.  Feel free to post here if you have
questions along the way.

.......................................................................
I've wanted to start small but all the part gets interconnected in
such a way that i end up writing a huge program that doesn't run at
all

..................................................................................
 
M

Maxx

On 04/ 2/11 08:13 PM, Maxx wrote:
If you want people to test your code, don't post it with line numbers!

Here i've posted the program without line numbers::

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX_LEN 1000
char *xml[MAX_LEN]={
              "<item>",
                   "   <Class></Class>",
                   "   <Country></Country>",
                   "   <Name></Name>",
                   "   <Logtime></Logtime>",
                   "   <Globaltime></Globaltime>",
              "</item>"
    };

char class[MAX_LEN],country[MAX_LEN],*name_list[MAX_LEN],*name;
int logtime[MAX_LEN],*lg,no_of_player=0;
static int globaltime=0,flag=5;
FILE *input,*output;
void parsing_line(char *buf);
void write_xml(int tim);
void write_file(char **buff);
void insert_xml(char *line,char *replace);
int convert_to_sec(int value);

int main(int argc,char *argv[])
{

         int times=(*argv[2])-'0';
         char line[MAX_LEN],name_out[20];
         sprintf(name_out,"%s.readytohit",argv[1]);                 /
*name of output file */

         if(argc<1)
         {
             fprintf(stderr,"Too few arguments");
             exit(1);
         }
        if((input=fopen(argv[1],"r"))==NULL)
        {
             fprintf(stderr,"Can't open file %s",argv[1]);
             exit(2);
        }
        if((output=fopen(name,"w"))==NULL)
        {
            fprintf(stderr,"Can't open file %s",name);
            exit(2);
         }
LOOP:   while(fgets(line,MAX_LEN,input)!=NULL)
             {

                 if(strcmp(line,"Class:::")==0)
                          {
                              fgets(line,MAX_LEN,input);

strcpy(class,line);                             /* copy line to class
*/
                              goto LOOP;
                       } else if(strcmp(line,"Country:::")==0) {
                               fgets(line,MAX_LEN,input);

strcpy(country,line);                          /*copy line to country
*/
                               goto LOOP;
                    }else if(strcmp(line,"Globaltime:::")==0){
                               fgets(line,MAX_LEN,input);

globaltime=atoline;                            /*store the globaltime
*/
                               goto LOOP;
                    }else if(strcmp(line,"Name:::")==0){
                             goto LOOP;
             }

           parsing_line(line);
        }

          write_xml(times);
         fclose(input);
         fclose(output);
         return 0;
    }

void parsing_line(char *buf)                                     /*
This function segregates the line into tokens */
 {
         name=strtok(buf,";" );
        *lg=convert_to_sec((int)(strtok(buf,";")));
        name++,lg++,no_of_player++;
  }

void write_xml(int tim)                                           /
*this function prepares the xml  */
 {
         int i,j;
         for(i=0;i<tim;i++)
         {
             name=*name_list;
             lg=logtime;
             for(j=0;j<no_of_player;j++,name++,lg++)
             {
                 insert_xml(xml[1],class);
                 insert_xml(xml[2],country);
                 insert_xml(xml[3],name);
                 insert_xml(xml[4],(char *)lg);
                 insert_xml(xml[5],(char *)globaltime);
                 globaltime+=(*lg);
             }
             write_file(xml);
         }
     }

void insert_xml(char *line,char *replace)         /*this function
insert the values in between the xml fields */
{
        char *w,*linept,temparr[MAX_LEN];
        linept=line,w=temparr;
        while(*linept++!=replace[0])
        {
            *w++=*linept;
        }*w='\0';
        while(*replace!='\0')
        {
            *linept=*replace;
            linept++,replace++;
        }*linept='\0';
        strcat(line,w);
    }

void write_file(char **buff)           /* this function writes the
final xml to file */
{
         int i;
         for(i=0;i<18;i++)
         {

             fputs(buff,output);
         }
 }

int convert_to_sec(int value)
{
         int m;
         m=value/100;
         m=m*60+(value/100);
         return m;
 }




Here i've posted the program without line numbers::

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_LEN 1000


char *xml[MAX_LEN]={
"<item>",
" <Class></Class>",
" <Country></Country>",
" <Name></Name>",
" <Logtime></Logtime>",
" <Globaltime></Globaltime>",
"</item>"
};

char class[MAX_LEN], country[MAX_LEN], *name_list[MAX_LEN], *name;
int logtime[MAX_LEN], *lg, no_of_player=0;

static int globaltime=0, flag=5;
FILE *input, *output;

void parsing_line(char *buf);
void write_xml(int tim);
void write_file(char **buff);
void insert_xml(char *line,char *replace);
int convert_to_sec(int value);

int main(int argc, char *argv[])
{

int times=(*argv[2])-'0';
char line[MAX_LEN], name_out[20];
sprintf(name_out,"%s.readytohit",argv[1]); /
*name of output file */

if(argc<1)
{
fprintf(stderr,"Too few arguments");
exit(1);
}

if((input=fopen(argv[1],"r"))==NULL)
{
fprintf(stderr,"Can't open file %s",argv[1]);
exit(2);
}

if((output=fopen(name,"w"))==NULL)
{
fprintf(stderr,"Can't open file %s",name);
exit(2);
}

LOOP: while(fgets(line,MAX_LEN,input)!=NULL)
{

if(strcmp(line,"Class:::")==0){
fgets(line,MAX_LEN,input);

strcpy(class,line);
goto LOOP;

} else if(strcmp(line,"Country:::")==0) {
fgets(line,MAX_LEN,input);

strcpy(country,line);
goto LOOP;

}else if(strcmp(line,"Globaltime:::")==0){
fgets(line,MAX_LEN,input);

globaltime=atoi(line);
goto LOOP;

}else if(strcmp(line,"Name:::")==0){
goto LOOP;
}

parsing_line(line);
}

write_xml(times);
fclose(input);
fclose(output);
return 0;
}

void parsing_line(char *buf) /*

{
name=strtok(buf,";" );
*lg=convert_to_sec((int)(strtok(buf,";")));
name++, lg++, no_of_player++;
}

void write_xml(int tim) /

{
int i, j;

for(i=0;i<tim;i++)
{
name=*name_list;
lg=logtime;

for(j=0;j<no_of_player;j++,name++,lg++)
{
insert_xml(xml[1],class);
insert_xml(xml[2],country);
insert_xml(xml[3],name);
insert_xml(xml[4],(char *)lg);
insert_xml(xml[5],(char *)globaltime);
globaltime+=(*lg);
}
write_file(xml);
}
}

void insert_xml(char *line,char *replace)

{
char *w, *linept, temparr[MAX_LEN];
linept=line, w=temparr;

while(*linept++!=replace[0])
{
*w++=*linept;
}*w='\0';

while(*replace!='\0')
{
*linept=*replace;
linept++,replace++;
}*linept='\0';

strcat(line,w);
}

void write_file(char **buff)

{
int i;
for(i=0;i<18;i++)
{

fputs(buff,output);
}
}

int convert_to_sec(int value)
{
int m;
m=value/100;
m=m*60+(value/100);
return m;
}
 
Ad

Advertisements

B

Ben Bacarisse

Maxx said:
Maxx <[email protected]> writes:
018        char class[MAX_LEN],country[MAX_LEN],*name_list[MAX_LEN],*name;
This will never be true because fgets keeps the newline (if it can).

Yeah i can't figure out this part at all.fgets is supposed to get a
fresh line everytime its called, so in here i wanted it to get a fresh
line each time and store it in line[] and do the parsing on the line..

That's what happens. The big problems are elsewhere. To solve the
small issue of the newline, you'd have to

strcmp(line, "Class:::\n")

or remove the \n from line first.

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?

The inner loop is to insert the values in all the xml fields,so it's
supposed to loop through the no_of_players provided and print the xml
template and the outer loop is the no of time the user wants to write
the xml template

The main problem is then that name is of the wrong type. You want it to
loop though the name list but it is a char * variable rather than a char
**. However, it would be very much clearer just to index name_list
rather than use name (i.e. you write name_list[j]). That would mean
you'd have to use more parameters to your functions and you really should
start doing that anyway.

The recent code you've posed contains key parts comments out and in a
reply to that post you posted code with an unterminated comment.
Without a clean and reliable version of the source, commenting on it
feels like a waste of time.

sorry it will be 7 which is the number of rows in the xml array..

Had you written 7 I'd still have replied "7?". You need to either name
such things or calculate them.

Actually i was changing the original values when trying to debug the
program.. and ended up posting the debugged version here.. Got all
messed up

Eh? What compiler accepted the code you posted? It used undeclared
variables.

unfortunately i don't know anything else other than c and little of c+
+ and java

I don't know anyone who has regretted the time taken to learn a text
manipulation language like awk or perl. There are thousands of text
file re-writing jobs like this that take a few minutes using one of
these.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top