Inserting integers into a character array

This is a discussion on Inserting integers into a character array within the Windows Programming forums, part of the Platform Specific Boards category; greetings I am trying to insert various different integers in different locations within a character array. sprintf_s works fine but ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    13

    Inserting integers into a character array

    greetings I am trying to insert various different integers in different locations within a character array.

    sprintf_s works fine but only to insert at the start of the array.

    I can't for example use array[6] to point where I want the integer to be inserted in the first parameter.

    What can I use to point to the location within the array and have an integer placed there?

    Thanks for you time.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    Why not?

    sprintf(&array[6], "%d", myInt );

    should be fine.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    13
    Maybe I should have included I am working on a Dialog based app under MFC if it makes any difference but using sprintf causes the following error message...

    error C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead

    If I try to use sprintf_s I the get the following error message...

    error C2664: 'int sprintf_s(char *,size_t,const char *,...)' : cannot convert parameter 2 from 'const char [3]' to 'size_t'

    ?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    Yeah, okay. sprintf() is specified in the C standard. sprintf_s() is a Microsoft proprietary "safe" alternative.

    Microsoft provided an ostensibly safe alternative as sprintf_s(), and configured their compilers to complain about sprintf() as you are seeing. sprintf_s() works essentially like sprintf(), except that the second parameter is the maximum length of buffer being written to (the first argument), there is a bit more checking of the format string.

    Similar things have been done with several other functions in the C standard library.

    I use the words "ostensibly safe" because it is still possible to get into trouble with the Microsoft safe functions, albeit it is more difficult, and I have seen programmers get over-confident because they are using them (and make other simple mistakes that they don't check for, and therefore never detect).

    Some other vendors have also provided their own "safe" versions of C standard functions - albeit with different names and different behaviours than the Microsoft ones.

    Microsoft's error message about deprecation is incorrect, at least in terms of the standard. Deprecation is a term defined in the standard that means essentially "scheduled for removal from a future version of this standard". That is not the case here. Microsoft are using the word "deprecate" in the sense that they encourage using an alternative (so it is deprecated as far as Microsoft is concerned, and developers who are willing to be locked into Microsoft development products, but not for anyone else).


    Given that you are using MFC (which is a vendor specific product from Microsoft) I can guess you are happy to be limited to Microsoft's development environments. In that case, it is up to you whether you use the C standard functions (such as sprintf()) or the Microsoft safe versions (such as sprintf_s()). If you care at all about portability (being able to build your code with a different compiler, on a different operating system) you will need to stick to sprintf() .... with all its warts.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    13
    Appreciate your time mate but quite honestly I don't give a toss on what I use I just want to do the job.

    I stick to Assembly language mostly no headaches like this but turn to MFC Dialog based apps as a quick (normally) way to get something done at the end of a project.

    A quick and easy way (I don't care if it is dirty) to stuff integer values into a character string array and stop the compiler whinging would be excellent.

    Thanks again for your time.

  6. #6
    Registered User
    Join Date
    Mar 2013
    Posts
    13
    Success this is how I did it.

    I would very much appreciate any tips on improvements as I know my coding is pretty sloppy

    thanks in advance

    Code:
    
        char name[81], second_digit[4], third_digit[4], final_key[80];
        int len = 0x0, init_num;
    
    
        GetDlgItemText(IDC_EDIT1, name, 80);
    
        len = strlen(name);
    
        init_num = len + 6;
    
        sprintf_s(final_key, "%d", init_num);
    
        strcat_s(final_key, "-");
    
        sprintf_s(second_digit, "%d", len);
    
        strcat_s(final_key, second_digit);
    
        
    
    
        if (len < 16)
        {
            strcat_s(final_key, "--");
    
            sprintf_s(third_digit, "%d", 15-len);
            strcat_s(final_key, third_digit);
            strcat_s(final_key, "-");
        }
        else
        {
            strcat_s(final_key, "-");
            sprintf_s(third_digit, "%d", len-15);
            strcat_s(final_key, third_digit);
            strcat_s(final_key, "-");
        
        }
    
        strcat_s(final_key, name);
        strcat_s(final_key, "-Art");
    
    
        SetDlgItemText(IDC_EDIT2, final_key);

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Quote Originally Posted by ice_cracked View Post
    Appreciate your time mate but quite honestly I don't give a toss on what I use I just want to do the job.
    That's not our problem. You were given explanations and advice on what you use, so it's your job to interpret that and choose the best solution for you.

    I stick to Assembly language mostly no headaches like this but turn to MFC Dialog based apps as a quick (normally) way to get something done at the end of a project.
    Assembly? In 2013? For programs, and not some optimized algorithm? I want to smack you.
    You need to get off your high horse and learn C or C++, whichever suits your purposes more.
    But I seriously fail to understand why the heck you are using MFC with C. You are using a C++ framework, so why the heck not use C++?
    If you insist on sticking with C, then why don't you get a C library?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    You do code in a strange mix of C / C++. Reminds me of the digital image processing code I worked on in the 90's.

    Why not something like...

    Code:
    len = strlen(name); 
    
    if(len < 16)	
    {
    	_snprintf( final_key, 79, "%d-%d--%d-%s-Art",(len+6) ,len ,(15-len), name );
    }
    else
    {
    	_snprintf( final_key, 79, "%d-%d-%d-%s-Art",(len+6) ,len ,(len-15), name );
    }
    
    SetDlgItemText(IDC_EDIT2, final_key);
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  9. #9
    Registered User
    Join Date
    Mar 2013
    Posts
    13
    Quote Originally Posted by Elysia View Post
    You need to get off your high horse and learn C or C++, whichever suits your purposes more.
    No high horse I assure you and no one here to try and impress I just enjoy assembler and have for years.

    Quote Originally Posted by Elysia View Post
    But I seriously fail to understand why the heck you are using MFC with C. You are using a C++ framework, so why the heck not use C++?
    No reason I just always have and don't know any better and no one to teach me except books and online resources?

    Quote Originally Posted by Elysia View Post
    If you insist on sticking with C, then why don't you get a C library?
    Is there one available which includes a resource editor to knock out dialogs etc quickly?
    If so any suggestions are most welcome and I will check them out.
    Also as I am not doing anything for DOS only Windows should I stick to C or C++?

  10. #10
    Registered User
    Join Date
    Mar 2013
    Posts
    13
    Quote Originally Posted by novacain View Post
    Why not something like...

    Code:
    len = strlen(name); 
    
    if(len < 16)    
    {
        _snprintf( final_key, 79, "%d-%d--%d-%s-Art",(len+6) ,len ,(15-len), name );
    }
    else
    {
        _snprintf( final_key, 79, "%d-%d-%d-%s-Art",(len+6) ,len ,(len-15), name );
    }
    
    SetDlgItemText(IDC_EDIT2, final_key);
    I very much like that thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing integers to a character array
    By C0__0D in forum C Programming
    Replies: 4
    Last Post: 01-30-2013, 08:57 PM
  2. Replies: 3
    Last Post: 03-26-2012, 10:50 AM
  3. Problem with inserting Character.
    By Caerleon in forum C Programming
    Replies: 7
    Last Post: 11-13-2009, 06:39 AM
  4. Help! Inserting a character into file
    By Ariod in forum C Programming
    Replies: 16
    Last Post: 11-21-2004, 03:32 PM
  5. Inserting a character in the middle of a string
    By winsonlee in forum C Programming
    Replies: 1
    Last Post: 03-20-2004, 03:35 AM

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