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

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

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

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

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

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

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

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.

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

"Keith Thompson" <> wrote in message
news:...
> Maxx <> 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.

"BartC" <> writes:
> "Keith Thompson" <> wrote in message
> news:...
>> Maxx <> 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) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

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

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

On Sat, 02 Apr 2011 10:33:13 -0700, Keith Thompson <>
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.

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

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

On Apr 2, 10:33 am, Keith Thompson <> wrote:
> Maxx <> 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.
>

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

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

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.

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

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

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

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.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!