0

I need help because I am not getting the expected output while attempting to read the command line arguments. It is really strange because I copied and pasted the code into a regular console application and it works as expected. It is worth noting that I am running Windows 7 and in visual studio I set the command line argument to be test.png

Win32 Code:

#include "stdafx.h"

using namespace std;

int _tmain(int argc, char* argv[])
{
    //Questions: why doesn't this work (but the one in helloworld does)
    //What are object files? In unix I can execute using ./ but here I need to go to debug in top directory and execute the .exe
    printf("hello\n");
    printf("First argument: %s\n", argv[0]);
    printf("Second argument: %s\n", argv[1]);

    int i;
    scanf("%d", &i);

    return 0;
}

Output:

hello
First Argument: C
Second Argument: t

I tried creating a simple console application and it works:

#include <iostream>

using namespace std;

int main(int arg, char* argv[])
{
    printf("hello\n");
    printf("First argument: %s\n", argv[0]);
    printf("Second argument: %s\n", argv[1]);

    int i;
    scanf("%d", &i);

    return 0;
}

Output:

hello
First Argument: path/to/hello_world.exe
Second Argument: test.png

Does anyone have any idea what is going on?

user2316667
  • 5,444
  • 13
  • 49
  • 71
  • 3
    At a guess I would say you have a Unicode build but are trying to print the strings as if they are Ansi. – Jonathan Potter Feb 03 '14 at 02:56
  • @JonathanPotter How does *that* happen and still link? Or is that the issue in the first place (doesn't build)? I agree with you, i just don't see how it even builds. odd. – WhozCraig Feb 03 '14 at 02:58
  • @WhozCraig: Me either, although there's no guarantee the code shown here is *actually* the code being compiled. – Jonathan Potter Feb 03 '14 at 03:00
  • It is being compiled...I didn't do anything major. These are the templates provided by visual studio 2013. I literally just wrote the printf inside. – user2316667 Feb 03 '14 at 03:00
  • @JonathanPotter as usual, excellent point. – WhozCraig Feb 03 '14 at 03:00
  • why are using stdio and C++.iostream perhaps? – Ed Heal Feb 03 '14 at 03:01
  • @user2316667 If Mr.Potter is correct (and it certainly seems likely) change `_tmain` to `main()`, and change your project settings on the main project-config page to be MultiByte rather than Unicode. then rebuild the entire project. Make sure to make that change for *all* build configurations (debug and release). – WhozCraig Feb 03 '14 at 03:02
  • I changed it to stdio, builds and same, correct result. – user2316667 Feb 03 '14 at 03:04
  • For future readers: I messed up. I actually did change the main function from TCHAR* argv[] to char* argv[] for some reason. So it was NOT the original template. Not that that was really my problem anyway. – user2316667 Feb 03 '14 at 03:30

1 Answers1

3

_tmain is just a macro that changes depending on whether you compile with Unicode or ASCII, if it is ASCII then it will place main and if it is Unicode then it will place wmain

If you want the correct Unicode declaration that accepts command line arguments in Unicode then you must declare it to accept a Unicode string like this:

int wmain(int argc, wchar_t* argv[]);

You can read more about it here

Another issue with your code is that printf expects an ASCII C Style string and not a Unicode. Either use wprintf or use std::wcout to print a Unicode style string.

#include <iostream>
using namespace std;

int wmain(int argc, wchar_t* argv[])
{
    //Questions: why doesn't this work (but the one in helloworld does)
    //What are object files? In unix I can execute using ./ but here I need to go to debug in top directory and execute the .exe
    std::cout << "Hello\n";
    std::wcout << "First argument: " << argv[0] << "\n";
    std::wcout << "Second argument: " << argv[1] << "\n";

    return 0;
}
Community
  • 1
  • 1
Caesar
  • 9,483
  • 8
  • 40
  • 66
  • What is the difference? – user2316667 Feb 03 '14 at 03:02
  • I changed the main function to the one you described but I still only get the first letter. Great link though. – user2316667 Feb 03 '14 at 03:13
  • @user2316667 Check your settings that you are compiling it using Unicode. If using Visual Studio then right click the project, `properties` and in the `General` tab make sure `Character Set` is set to `Use Unicode Character set`. – Caesar Feb 03 '14 at 03:16
  • Yes. It was always set to Use Unicode Character Set. [edit] Not that I knew - just looked at it. And still the same problem. Note that the function works with main. But I'm really curious about getting it to work with unicode. I mean, it should be working, right? – user2316667 Feb 03 '14 at 03:17
  • As per the link, as expected, win32 application works perfectly when I use the main function. This may be complete misuse of terminology, but does one 'enter' in unicode or multibyte character set? – user2316667 Feb 03 '14 at 03:21
  • @user2316667 Yes, `printf` expects an ASCII C-Style string. – Caesar Feb 03 '14 at 03:24
  • For its identifier, that is correct. So it must not have read "%s" incorrectly and printed out a byte instead. There exists wprintf though. Works like a charm. Apparently append L infront of literal strings. I really appreciate your time Caesar. Thanks so much. – user2316667 Feb 03 '14 at 03:27