Hey all, recently I've played around with pointers and some win32 API and I've came up with this portion of code, to basically load a set of string from a file on a secondary storage, store it in the memory and then print it on the screen. The apparent problem is that this code draws slowly comparatively with printf function. I don't want to use printf because i am limited to the coordinates (0,0) only. I want to be able to print anywhere on the screen preserving the format of my string. So basically this function is able to do it but doing it slowly. This happen only if the string happens to be large, like maybe 4kb? So I am wondering is there anyway that i can optimize it? I am suspecting that the problem comes from the while loop that I used to individually scan the string for any escape character inorder to process new line therefore to preserve the user-defined coordinates. (My function printSTR allows the user to print the string on a specific location of a screen instead of printf which only prints from (0,0). )
Code:
//////////////////////////////////////////////////////////////////////////////
// void printSTR(char *text, int x, int y)
// Author: -aaronljx-
//
// To print a string to the console screen from a file preferably, you need
// to call the function LoadSTR first before printing it.
//
// Usage:
// char *text -> pointer to the address of the string
// int x, int y -> coordinates of where the string to be printed
//
// eg.
// printSTR("This is to print directly", 2, 5);
//
// char *pStr1;
// pStr1 = LoadSTR("file1.txt");
// printSTR( pStr1, 2, 5 );
// free(pStr1);
//
// Note: Make sure you free every string loaded from file with
// the free function, free(var_name);
//////////////////////////////////////////////////////////////////////////////
void printSTR(char *text, int x, int y)
{
gotoXY(x, y);
while ( *text )
{
if ( *text == 10 ) // if *text returns \n
{
y++;
gotoXY(x, y);
text++;
}
else
{
printf("%c", *text);
text++;
}
}
return;
}
//////////////////////////////////////////////////////////////////////////////
// char *LoadSTR(char *filename)
// Author: -aaronljx-
//
// This function works hand in hand with the function printSTR
// to load string directly from file and print it on the screen
// on the given coordinates.
//
// Usage:
// char *filename -> the location of the string in your secondary storage
//
// Returns:
// -> the address of the loaded string
// eg.
// char *pStr1;
// pStr1 = LoadSTR("file name.txt");
// printSTR( pStr1, 2, 5 );
// free(pStr1);
//
// Note: Each time you load a string from file, make sure you free it
// after using it. This is important.
//////////////////////////////////////////////////////////////////////////////
char *LoadSTR(char *filename)
{
FILE *buffer;
char *pString, temp;
int nChar = 0;
if ( !( buffer = fopen( filename, "r" ) ) )
{
printf("\aError! Unable to open required files.\n");
getchar();
exit(EXIT_FAILURE);
}
while ( fscanf( buffer, "%c", &temp ) != EOF )
nChar++;
pString = (char*) calloc( nChar + 1, sizeof(char) );
rewind(buffer);
fscanf( buffer, "%[^\0]", pString );
fclose(buffer);
return pString;
}