Skip to main content
GameDev.net gamedev.net
🔒 Locked

Small C problem Odd Issue

Started by Kem Nov 24, 2003 at 5:30 PM 7 replies 800+ views
Original Post
Kem
Kem
Ok I''m converting an old C program and decided to change a const data table into an XML file. I can open and close everything fine, it has nothing to do with the file part as you''ll see. The following is giving me issues... Here is a function that I am using. Notice the line "int vTemp;" is commented out.
void fCreateSkillFile()
{
	FILE *fp;

	fp = fopen( "skillfile.xml", "w");

	fprintf(fp, "<SkillList>\n\r");

	
/*	int vTemp;
	for(int vTemp = 0; vTemp < MAX_SKILL;vTemp++)
	{
		fprintf(fp,"<Skill>\n\r");
		fprintf(fp,"<SkillName>%s</SkillName>", skill_table[vTemp].name);
		fprintf(fp,"</Skill>\n\r");
	};
*/
	fprintf(fp, "</SkillList>");
}
The above compiles FINE. No problems at all. If I uncomment the line "int vTemp;" by moving the /* down one line. It won''t compile saying "missing ";" before ''type''" Still looking and trying but figure out whats wrong (that function is the only thing added to the code that already works on its own). But quite honestly I can''t think of what on earth its trying to tell me.
Kem
Kem
OMG.... Nevermind.

Apparently I need to have braces around it all as such.


void fCreateSkillFile()
{
FILE *fp;

fp = fopen( "skillfile.xml", "w");

fprintf(fp, "<SkillList>\n\r");


{
int vTemp;

for ( vTemp = 0; vTemp < MAX_SKILL; vTemp++ )
{
fprintf(fp,"<Skill>\n\r");
fprintf(fp,"<SkillName>%s</SkillName>", skill_table[vTemp].name);
fprintf(fp,"</Skill>\n\r");
};
};
fprintf(fp, "</SkillList>");
fclose(fp);
}


Now the question is why does this work and the above not work?
Kem
Kem
Ahhhh. Thank you very much now I''ll know how to avoid that issue later on

Normally I''m in C++/C# so I forgot about that with C...

Thanks again
Kem
Kem
quote:
Original post by Anonymous Poster
this is one of my pet peeves about C, although now you can just feed C code to a C++ compiler and you''ve got the best of both worlds. (of course, there are tons of reasons why you might not be able to do that in practice.)




The ''Mud'' code I''m converting right now doesn''t take kindly to just compiling it as C++ however. It is rather... Hostile to it in general. (At least 40+ errors last I tried, but that was years ago)

ALthough it did show me back in the day that C/C++ are different animals enough to keep seperate in thought
Chris Hare
Chris Hare
Your original post was declaring vTemp twice, once in the loop, once before.

I''d say your next post compiles (more) because you eliminated it, not because of the braces, but I''m not familiar with C rules, only C++.

Wizza Wuzza?
Evil Steve
Evil Steve
A litte off topic:
You should end lines with "\r\n" or "\n", not "\n\r". It doesn''t really matter so long as you are consistant, but all windows text editors use "\r\n" or "\n" (99% of the time "\r\n")
Aldacron
Aldacron
It''s worth noting that with a C99 compliant compiler the first version would work fine. C99 allows for variable declarations mixed with code, just as C++ does. The latest versions of GCC support it, VC 6 does not (it was pre-C99). I would imagine VC7 does, but I don''t have it so I can only guess.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.