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

String again... (Win32 Console)

Started by MrSandman666 Oct 20, 2000 at 8:58 PM 1 replies 150+ views
Original Post
MrSandman666
MrSandman666
I''m sorry to bother you again with this but this just drives me crazy! So I beg you to pleeeeaaaze have a look at this and help me if possible. I have the following code:
    
void WriteStringToFrame(Frame frame, const char *String, ...)
{
	va_list argument;
	int counter;
	
	char *buffer;
	CHAR_INFO *str;

	buffer = new(char[frame.Size.X * frame.Size.Y]);
	str = new(CHAR_INFO[frame.Size.X * frame.Size.Y]);
	
	memset(&buffer, 0, sizeof(buffer));
	memset(&str, 0, sizeof(str));

	va_start(argument, String);
	vsprintf(buffer, String, argument);
	va_end(argument);
	
	for (counter = 0; counter <= sizeof(buffer); counter++)
		str[counter].Char.AsciiChar = buffer[counter];

	for (counter = 0; counter <= sizeof(buffer); counter++)
		str[counter].Attributes = Color;

	WriteConsoleOutput( 
        hOutput,
        str,
        frame.Size,
        frame.Pos,
        &frame.Rect);
};
    
There are quite a few variables declared outside the function, so don''t bother about the declarations. They are all declared. The code compiles fie without errors and wanrings but when I execute it, I get an assertion error from vsprintf. If it is of interest, the message gives these infos: File: vsprintf.c Line: 90 Expression: string != NULL What the hell is wrong? Can you help me? Pleaaaaaazze! I''m desperate! "Mr Sandman bring me a dream"
-----------------------------"Mr Sandman bring me a dream"
felisandria
felisandria
vsprintf. Eww.

Okay. It's been a really long time since I've seen this, but I think you might be having trouble with your va arguments. Take a look in your MSDN help for further info, but, va arguments are supposed to take the va_list being set up, and the type of the next thing going into your list. So, I'm going to cut/past the MSDN help here, and I think maybe you can see what's going on:

This sets up your vsprintf to handle a list of integers:
    
va_start( marker, first ); /* Initialize variable arguments. */
while( i != -1 )
{
sum += i;
count++;
i = va_arg( marker, int);
}
va_end( marker ); /* Reset variable arguments. */



first, in this case, was declared as an integer.

You're using some type called String, and none of the stuff you're passing around seems to be of that type. Unless of course it's the String you passed in, in which case it can't really determine the size properly from a char* most likely. You need to tell it how big, because the point of the va list is to tell it how much memory to allocate for each argument. Also, I don't think you can call vsprintf before you end your va list, if I remember correctly. (It's been a really long time. I might not remember accurately.)

Why are you using vprintf anyway? Why not something easy like printf and scanf?

HTH
-fel

*edit: I can never remember how these source tags work.


Edited by - felisandria on October 21, 2000 3:14:20 AM
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
MrSandman666
MrSandman666
Update:
the function works now... more or less...
I played around with the code a little, visited MSDN and finally it printed what I wanted.
This is the code:
    
void gengine::WriteStringToFrame(Frame frame, const char *String, ...)
{
va_list argument;
int counter;
char *str;
CHAR_INFO *output;
COORD Size;

output = new CHAR_INFO[strlen(String)];
str = new char[strlen(String)];

va_start(argument, String);
vsprintf(str, String, argument);
va_end(argument);

Size.X = strlen(str);
Size.Y = 1;

for (counter = 0; counter <= (frame.Size.X * frame.Size.Y); counter++)
output[counter].Char.AsciiChar = str[counter];

for (counter = 0; counter <= (frame.Size.X * frame.Size.Y); counter++)
output[counter].Attributes = Color;

WriteConsoleOutput(hOutput, output, Size, frame.Pos, &frame.Rect);
}


Edited by - MrSandman666 on October 21, 2000 2:01:15 PM
-----------------------------"Mr Sandman bring me a dream"

Topic Locked

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

Sign in to reply to this topic.