[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

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

Maxx

4/2/2011 7:14:00 AM

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[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 }



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.
15 Answers

Ian Collins

4/2/2011 7:22:00 AM

0

On 04/ 2/11 08:13 PM, Maxx wrote:
>
> 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!

--
Ian Collins

Ben Bacarisse

4/2/2011 10:17:00 AM

0

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.

Keith Thompson

4/2/2011 5:33:00 PM

0

Maxx <grungeddd.maxx@gmail.com> writes:
> 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-fa... is a valuable resource.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Bartc

4/3/2011 12:23:00 AM

0

"Keith Thompson" <kst-u@mib.org> wrote in message
news:ln8vvs4kyu.fsf@nuthaus.mib.org...
> Maxx <grungeddd.maxx@gmail.com> writes:

>> 001 #include<stdio.h>
>> 002 #include<string.h>

> 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?)

--
Bartc



Keith Thompson

4/3/2011 2:58:00 AM

0

"BartC" <bc@freeuk.com> writes:
> "Keith Thompson" <kst-u@mib.org> wrote in message
> news:ln8vvs4kyu.fsf@nuthaus.mib.org...
>> Maxx <grungeddd.maxx@gmail.com> writes:
>
>>> 001 #include<stdio.h>
>>> 002 #include<string.h>
>
>> 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.

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

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Willem

4/3/2011 9:31:00 AM

0

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

James Kuyper

4/3/2011 11:51:00 AM

0

On 04/03/2011 05:30 AM, Willem wrote:
> 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.
--
James Kuyper

Geoff

4/3/2011 5:48:00 PM

0

On Sat, 02 Apr 2011 10:33:13 -0700, Keith Thompson <kst-u@mib.org>
wrote:

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

Sherm Pendley

4/4/2011 4:01:00 AM

0

Willem <willem@toad.stack.nl> writes:

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

Possibly - but much less so than a comment such as "// <-- Error here!"
that will actually compile without major surgery.

sherm--

>
>
> SaSW, Willem

--
Sherm Pendley
<http://camelbones.sourcefor...
Cocoa Developer

Maxx

4/4/2011 6:38:00 AM

0

On Apr 2, 12:21 am, Ian Collins <ian-n...@hotmail.com> wrote:
> On 04/ 2/11 08:13 PM, Maxx wrote:
>
>
>
> > 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!
>
> --
> Ian Collins

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