_snprintf

This is a discussion on _snprintf within the C++ Programming forums, part of the General Programming Boards category; I have some code Code: char szSub[255]; _snprintf( szSub, sizeof szSub, "SQ.%s. .CSCO", m_szPart ); where Code: char m_szPart[255]; I ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    31

    _snprintf

    I have some code

    Code:
           char szSub[255];
    
    	_snprintf(	szSub, 
    		sizeof szSub, 
    		"SQ.%s. .CSCO",
    		m_szPart 
    		);
    where
    Code:
    char m_szPart[255];
    I want to change this function so I pass in a string which is used to build the string that is being built here. For instance...
    rather then CSCO in SQ.%s. .CSCO I could pass in MSFT and SQ.%s. .MSFT would be built. How could I do this?
    Last edited by mhandlon; 04-10-2006 at 07:49 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    So do
    "SQ.%s. .%s",

    And pass a 2nd string to your function as either "CSCO" or "MSFT"
    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
    Jan 2006
    Posts
    31
    ok I passed in the string ss but when I debug..

    Code:
    	_snprintf(	szSub, 
    		sizeof szSub, 
    		"SQ.%s. .%s",
    		m_szPart,
    		ss
    		);
    Is showing up as
    + szSub 0x0012f8d4 "SQ.TEST. .(null)" char [255]

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Did you make ss point at anything, or just declare it?

    char * ss;
    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.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    31
    It's declared as a string, I decided the call it sSymbol instead ss not a good name.

    Code:
    bool PriceTest::GetFeed(string sSymbol)
    {
    
    	printf( "example is using CSCO as a stock symbol\n" );	
    
    	char szSub[255];
    
    	_snprintf(	szSub,
    		sizeof szSub, 
    		"SQ.%s. .%s",
    		m_szPart,
    		sSymbol
    		);
    From main the function is being called from...
    Code:
    PriceTest tc;
    
    tc.GetFeed("CSCO");
    I checked and sSymbol is assigned the value “CSCO” ?
    What am I missing?

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    The fact that your compiler is probably generating a warning?

    The fact that printf style functions, when seeing a %s expect a char*, not a c++ string?

    Why are you even messing with snprintf when you have C++ strings to start with?

    If you're still insistent on old-school, try
    Code:
    _snprintf(	szSub,
    		sizeof szSub, 
    		"SQ.%s. .%s",
    		m_szPart,
    		sSymbol.c_str()  // get  a const char * from the c++ string
    		);
    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.

Popular pages Recent additions subscribe to a feed

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