s(n)printf weird things happening...

This is a discussion on s(n)printf weird things happening... within the Windows Programming forums, part of the Platform Specific Boards category; Running code like this, weird things happen: Code: char SBD_FULL_PATH[1024]; /* a ways down */ printf("SFP1: %s\n",SBD_FULL_PATH); snprintf(SBD_FULL_PATH,sizeof(SBD_FULL_PATH), "%s\%s%s\"",SBD_FULL_PATH,argv[0],SBD_EXTENSION); printf("SFP2: ...

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

    s(n)printf weird things happening...

    Running code like this, weird things happen:
    Code:
    char SBD_FULL_PATH[1024];
    /* a ways down */
    printf("SFP1: %s\n",SBD_FULL_PATH);
    snprintf(SBD_FULL_PATH,sizeof(SBD_FULL_PATH),
        "%s\\%s%s\"",SBD_FULL_PATH,argv[0],SBD_EXTENSION);
    printf("SFP2: %s\n",SBD_FULL_PATH);
    snprintf(SBD_FULL_PATH,sizeof(SBD_FULL_PATH),
        "\"%s",SBD_FULL_PATH);
    printf("SFP3: %s\n",SBD_FULL_PATH);
    Output:
    Code:
    SFP1: C:\folder
    SFP2: C:\folder\file.txt"
    SFP3: """""""""""""""""""
    Is snprintf or _snprintf in Windows broken? (Yes, both functions output the same thing.) Using MinGW on Windows XP.
    Sigh, nothing ever works the first try.

    Register Linux User #314127

  2. #2
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Can't see anything wrong, although one of your string literals has an extra \" at the end, which is producing this output:

    Code:
    SFP2: C:\folder\file.txt"
    Is it possible for you to post the source code for a program that demonstrates the said problem? It would be easier for us to compile and test and see if snprintf is really broken.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    42
    Yes, I want to quote the string, so I broke what i was doing into parts.

    Code:
    #include <stdio.h>
    #include <windows.h>
    
    int main(int argc, char **argv)
    {
    	char str[1024];
    	GetCurrentDirectory(sizeof(str),str);
    	printf("Str1: %s\n",str);
    	_snprintf(str,sizeof(str),"\"%s\\%s\"",str,argv[0]);
    	printf("Str2: %s\n",str);
    	return 0;
    }
    Sample output:
    Code:
    Str1: C:\folder
    Str2: """"""""""\C:\folder\test.exe"
    Last edited by KneeLess; 10-15-2005 at 05:46 PM.
    Sigh, nothing ever works the first try.

    Register Linux User #314127

  4. #4
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Well, perhaps your problem lies in using SBD_FULL_PATH as the output and input.

    Code:
    snprintf(SBD_FULL_PATH,sizeof(SBD_FULL_PATH),  "\"%s",SBD_FULL_PATH);

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    42
    Yes, that's the problem, I wonder why that happens.
    Sigh, nothing ever works the first try.

    Register Linux User #314127

  6. #6
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    From the documentation

    Quote Originally Posted by MSDN
    If copying occurs between strings that overlap, the behavior is undefined.
    SBD_FULL_PATH definitely overlaps SBD_FULL_PATH.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Funny things happening outside of function
    By drb2k2 in forum C++ Programming
    Replies: 1
    Last Post: 04-08-2003, 02:26 PM
  2. Doing things like VB using Windows API in C++
    By conright in forum Windows Programming
    Replies: 6
    Last Post: 01-05-2003, 11:51 AM
  3. clearing things up
    By webturtle0 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 11-28-2002, 02:59 PM
  4. Weird bug
    By Hunter2 in forum Windows Programming
    Replies: 1
    Last Post: 08-12-2002, 03:45 PM
  5. Help with these three things...
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 08-26-2001, 07:05 AM

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