Today I wrote this and it is not working. I only get the "He" output and not the "Hello" output. Help!
The code:
Code:
# include <stdio.h>
# include <windows.h>
# include <conio.h>
# include <string.h>
void Print (short x, short y, WORD Color, char *String, ...);
int main ()
{
// example how should user use this funtion
Print (0, 0, FOREGROUND_RED, "%s", "Hello");
return 0;
}
void Print (short x, short y, WORD Color, char *String, ...)
{
COORD screenPosition = {0};
WORD *ColorBuffer = 0;
DWORD dwResult = 0;
short Modulus = 0, StringLength = 0, i = 0;
va_list List;
char Temp[100] = {0};
va_start (List, String);
StringLength = (short) strlen (String);
screenPosition.X = x;
screenPosition.Y = y;
ColorBuffer = (WORD *) malloc (sizeof (WORD) * (StringLength + 1)); // +1 is for the NULL
for (i = 0; i < StringLength; i++)
{
ColorBuffer[i] = Color;
if (*(String+i) == '%') Modulus++;
}
for (i = 0; i < Modulus; i++)
{
if (*String++ == '%')
{
if (*String == 's')
{
sprintf (Temp, "%s", va_arg (List, char *));
}
else if (*String == 'd')
{
sprintf (Temp, "%d", va_arg (List, int));
}
String++;
}
}
WriteConsoleOutputAttribute (GetStdHandle(STD_OUTPUT_HANDLE), ColorBuffer, StringLength, screenPosition, &dwResult);
WriteConsoleOutputCharacter (GetStdHandle(STD_OUTPUT_HANDLE), Temp, StringLength, screenPosition, &dwResult);
free (ColorBuffer);
va_end (List);
}