Arguments in funtion - AGAIN

This is a discussion on Arguments in funtion - AGAIN within the C Programming forums, part of the General Programming Boards category; Hi! I really need this function so I'm asking you guys to help me solve this huge problem for me. ...

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Unhappy Arguments in function - AGAIN

    Hi!

    I really need this function so I'm asking you guys to help me solve this huge problem for me.
    This function should accept string (%s) and integer (%d) arguments. I can't get this working
    and this function has only the integer arguments. PLEASE HELP ME .

    This is what I have written so far ...
    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 %d", "This is me", 100);
        return 0;
    }
    void Print (short x, short y, WORD Color, char *String, ...)
    {
        COORD  screenPosition = {0};
        WORD   *ColorBuffer = 0;
        DWORD  dwResult = 0;
        HANDLE hOutput = 0;
        short  j = 0, Length = 0;
        int Integer = 0;
        va_list marker;
    	
    
        Integer = atoi (String);
        va_start (marker, Integer);     /* Initialize variable arguments. */
        while ( Integer != 0 )
        {
             Integer = va_arg (marker, int);
        }
        screenPosition.X = x;
        screenPosition.Y = y;
        hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
        sprintf (String, "%d", Integer);
        Length = (short) strlen (String);
    
        ColorBuffer = (WORD *) malloc (sizeof (WORD) * (Length + 1)); // +1 is for the NULL
    
        for (j = 0; j < Length; j++)
        {
            ColorBuffer[j] = Color;
        }
        WriteConsoleOutputAttribute (hOutput, ColorBuffer, Length, screenPosition, &dwResult);
        WriteConsoleOutputCharacter (hOutput, String, Length, screenPosition, &dwResult);
        free (ColorBuffer);
        ColorBuffer = 0;
        va_end (marker);
    }
    Last edited by GaPe; 09-25-2002 at 11:59 AM.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: Arguments in function - AGAIN

    Integer = atoi (String);

    waht is this line supposed to do. anyway, look up the refrence on vsprintf().
    hello, internet!

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685

    errrrrf

    Okay buddy I first have to ask you, what's wrong with using printf? Or sprintf?

    Anyways, here is a tiny bit of help.

    Code:
    #include <stdio.h>
    #include <stdarg.h>
    #include <string.h>
    
    int print_func(const char *string, ...) {
    	int modulus, len, i, j;
    	va_list list;
    	
    	modulus = 0;
    	len = strlen(string);
    	va_start(list, string);
    	
    	for(i = 0; i < len; i++)
    		if(*(string+i) == '%')
    			modulus++;
    
    	for(i = 0; i < modulus; i++) {
    		if(*string++ == '%') {
    			if(*string == 's') {
    				//do string stuff
    			} else if(*string == 'd') {
    				//do interger stuff
    			}
    			string++;
    		}
    	}
    	va_end(list);
    }
    You should get the idea. You should also just use sprintf() which works just like printf() but its output goes into a string buffer instead of a file.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    moi has a good point, vsprintf() would work best for your current function. Also, I did not pay any attention to the atoi(String) thing because I assumed either he thought that atoi returns string lenght or because he was going to format his strings like this
    "3 %s is %s for %d minutes"

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    As a side line point, remember that string is a reserved word, and shouldn't be used.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Hammer
    As a side line point, remember that string is a reserved word, and shouldn't be used.
    is it really?
    hello, internet!

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by moi
    is it really?
    Yes, or I wouldn't have said so
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    lol check out this guy's reply:
    "string ain't a reserved word, mama"

    Hahahahhah, so funny. He also unintentionally had prelude's gender right too.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    yeah... it was one of those arguments where you just new who'd win hands down, right from the off.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Hammer
    Yes, or I wouldn't have said so
    but that's string, this is String
    hello, internet!

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by moi
    but that's string, this is String
    Yep, I spotted that... but master5001 didn't
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    I don't like printf function beacuse it is very slow and always moves the screen cursor which is in my case very very bad. So I decided to create my own printf function which will be fast and will not move the screen cursor. This can be done with WINAPI functions.

    Thank you for the code. I will work on it now. If I won't understand something then I'll be back .
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  13. #13
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    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);
    }
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  14. #14
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by GaPe
    I don't like printf function beacuse it is very slow and always moves the screen cursor which is in my case very very bad. So I decided to create my own printf function which will be fast and will not move the screen cursor. This can be done with WINAPI functions.

    Thank you for the code. I will work on it now. If I won't understand something then I'll be back .
    printf is pretty fast and pretty complex...I cant see you really getting a function as good as that at a better efficiency....You can play around and build one yourself by all means, but I think this should do everything you are trying to accomplish......probably more efficiently too..it will reset the cursor and colour to the original state before the call....handy

    Code:
    void Print (short x, short y, WORD Color, char *String, ...)
    {
        va_list List;
    	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    	CONSOLE_SCREEN_BUFFER_INFO csbi = {0};
    	COORD coord = {x,y};
    
    	GetConsoleScreenBufferInfo(hOut,&csbi);
    	SetConsoleCursorPosition(hOut,coord);
    	SetConsoleTextAttribute(hOut,Color);
    	va_start (List, String);
    		vprintf(String,List);
    	va_end (List);
    	SetConsoleCursorPosition(hOut,csbi.dwCursorPosition);
    	SetConsoleTextAttribute(hOut,csbi.wAttributes);
    }

  15. #15
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by GaPe
    I don't like printf function beacuse it is very slow and always moves the screen cursor which is in my case very very bad. So I decided to create my own printf function which will be fast and will not move the screen cursor. This can be done with WINAPI functions.

    Thank you for the code. I will work on it now. If I won't understand something then I'll be back .
    printf is *fast*. quite fast.
    hello, internet!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  2. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  3. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  4. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM
  5. registry, services & command line arguments.. ?
    By BrianK in forum Windows Programming
    Replies: 3
    Last Post: 03-04-2003, 01:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21