Placing values inside stings

This is a discussion on Placing values inside stings within the C Programming forums, part of the General Programming Boards category; Hi, how do I incorporate a value inside a string ? Code: strncpy(buffer, input.usernbr, USERNBR_LEN); buffer[USERNBR_LEN] = '

Thread: Placing values inside stings

'; if(!isdigit(buffer) || ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    93

    Placing values inside stings

    Hi,

    how do I incorporate a value inside a string ?

    Code:
          strncpy(buffer, input.usernbr, USERNBR_LEN);
          buffer[USERNBR_LEN] = '\0';
    
          if(!isdigit(buffer) || is_all_spaces(input.usernbr, USERNBR_LEN))
          {
             strcpy(error_message, "EQ USERNBR INVALID - buffer\n");
             write_log_file(curtime,
    /* OUTPUT */
    ---------------------------------------------------------------
    Progname: eq_recon_mtn_cnx_format_eq
    Started: 2003/10/15 09:57:15 GMT
    File: R3AP200310_EQRSNACNXX_00001_20031014125499.SND
    ---------------------------------------------------------------

    ---> Number of Records Read: 1
    ---> Number of Records Written: 0

    EQ USERNBR INVALID - buffer

    ---------------------------------------------------------------
    Ended: 2003/10/15 09:57:15 GMT
    ---------------------------------------------------------------



    what I should be getting is

    EQ USERNBR INVALID - 85756999


    strcpy does not seem to accept format specifiers, so presumably I have to pass the value though anther function first ?


    tia,

  2. #2
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    You would want to use sprintf() to write a formatted string to an array of chars. It works like this sprint(buffer, Formatted_string, arg_list). It is like printf only it requires a pointer to char as well.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    93
    your right,

    this has done it for me

    Code:
    #define ERROR_MESSAGE       "EQ USERNBR INVALID - %s\n"
    
    
       if(error_value)
       {
          fprintf(fp, ERROR_MESSAGE, error_value);
          fprintf(fp, SPACES_ROW);
       }

    /* OUTPUT */
    ---------------------------------------------------------------
    Progname: eq_recon_mtn_cnx_format_eq
    Started: 2003/10/15 11:37:27 GMT
    File: R3AP200310_EQRSNACNXX_00001_20031014125499.SND
    ---------------------------------------------------------------

    ---> Number of Records Read: 7
    ---> Number of Records Written: 6

    EQ USERNBR INVALID - ZZZZZZZZ

    ---------------------------------------------------------------
    Ended: 2003/10/15 11:37:27 GMT
    ---------------------------------------------------------------



    thanks velius

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-30-2007, 09:08 AM
  2. reading values from a file
    By megastar in forum C Programming
    Replies: 4
    Last Post: 06-25-2007, 02:08 AM
  3. How to read in empty values into array from input file
    By wpr101 in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2002, 09:59 PM
  4. How-To: Load Pictures in MFC. Code Inside.
    By Xei in forum C++ Programming
    Replies: 1
    Last Post: 05-16-2002, 09:17 PM
  5. Parameter pass
    By Gades in forum C Programming
    Replies: 28
    Last Post: 11-20-2001, 01:08 PM

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