Does a function like this currently exist?

This is a discussion on Does a function like this currently exist? within the C++ Programming forums, part of the General Programming Boards category; i'm wondering if a function exists that basically appends a string in a formatted style. example: Code: int x = ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    9

    Does a function like this currently exist?

    i'm wondering if a function exists that basically appends a string in a formatted style. example:

    Code:
    int x = 50;
    int y = 100;
    char *blah = "that's great!";
    
    char *text;
    
    text = "Whee, you have ";
    
    //and here's where the function would be...
    //i'm making up a name for it ;) 
    buffer_printf (text,"%i apples and %i bananas! %s",x,y,blah);
    output being, "Whee, you have 50 apples and 100 bananas! that's great!"

  2. #2
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    yes, there is one, it's called sprintf, the first param is the buffer.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >char *text;
    Better give text some memory:
    char text[100];

    >buffer_printf (text,"%i apples and %i bananas! %s",x,y,blah);

    sprintf?
    sprintf (text,"Whee, you have %i apples and %i bananas! %s",x,y,blah);

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    9
    but doesn't sprintf fill the buffer, and not append to it?

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    sprintf (text + strlen(text),"%i apples and %i bananas! %s",x,y,blah);

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    strcat() will concatenate ( aka append) one C style string onto another.

  7. #7
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146

    Re: Does a function like this currently exist?

    Originally posted by IonBlade
    Code:
    int x = 50;
    int y = 100;
    char *blah = "that's great!";
    
    char *text;
    
    text = "Whee, you have ";
    
    //and here's where the function would be...
    //i'm making up a name for it ;) 
    buffer_printf (text,"%i apples and %i bananas! %s",x,y,blah);
    output being, "Whee, you have 50 apples and 100 bananas! that's great!"
    And then there's C++ output:
    Code:
    cout << text << x << " apples and " << y << " bananas! " << blah;
    If you have to do this repeatedly, you can write your own function that accepts some numbers and strings and outputs them in the correct order.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM

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