Printing out self path

This is a discussion on Printing out self path within the C++ Programming forums, part of the General Programming Boards category; Why does the following code print out only one character with printf and some hex code with cout when it ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    385

    Printing out self path

    Why does the following code print out only one character with printf and some hex code with cout when it is supposed to print out the string in s??
    Code:
     
        #include<iostream>
        #include<Windows.h>
        #include<stdio.h>
    	
    
        
        int main()
        {
        	TCHAR s[90];
    		GetModuleFileName(NULL,s,90);
    		
    		printf("%s",s);
    		std::cout<<s;
    		
    	}
    Last edited by juice; 03-15-2012 at 07:53 AM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2,510
    you're probably compiling with unicode character set enabled. try changing your compiler settings to compile using multi-byte strings instead of unicode.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,823
    Or, you know, use std::wcout. Can't guarantee it will print out anything eligible, though. The standard is broken in regards to wchar.
    Oh yeah, and NULL should be nullptr.
    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.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Elysia View Post
    Oh yeah, and NULL should be nullptr.
    If you're using c++11. Otherwise, old-style C++ practice is to simply use 0.

    IMO, you should set your character set to "not set" and get rid of things like TCHAR and TEXT, etc. Why complicate things if you don't need to?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,454
    It is also a really bad idea to mix usage of printf() and streaming to std::cout. In general, it is a bad idea to mix C and C++ style output to the same device or stream, as the two are not required to be synchronised.

    Whichever method you use, it does pay to flush the stream (or output a newline).

    Given that TCHAR is not necessarily the same as char, you need to be a bit more careful in selecting printf() format strings. Or convert the TCHAR's to char before outputting.

    Using nullptr is not a bad idea if your compiler supports C++-11. But that is a style thing, not a specific technical requirement (GetModuleFileName() is specifically documented as able to accept NULL, and this will work as the win32 API existed before nullptr did).

    You could also try printing out argv[0] (which is guaranteed to be a pointer to char), subject to a check that it is non-NULL. Bear in mind that argv[0] is not guaranteed to be a completely qualified path name.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need to specify the PATH in C
    By sunk22 in forum C Programming
    Replies: 2
    Last Post: 12-26-2011, 03:00 PM
  2. Printing non-printing characters in ^ and M- notation
    By sbeard22 in forum C Programming
    Replies: 6
    Last Post: 10-03-2008, 11:12 PM
  3. Help Please - Printing the path for links
    By lewiso in forum Linux Programming
    Replies: 9
    Last Post: 03-04-2008, 03:44 PM
  4. getting path of exe...
    By Rune Hunter in forum C++ Programming
    Replies: 8
    Last Post: 10-18-2004, 12:15 AM

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