2

Possible Duplicate:
What is the difference between _tmain() and main() in C++?

I have a console application, which takes one command line parameter.

int _tmain(int argc, char* argv[])
{
    ...
    printf("Path: %s\n", argv[1]);
        ...
}

When I run the program with an argument (myprogram.exe D:\myfolder\myfile), it prints Path: D instead of Path: D:\myfolder\myfile.

How should I change the procedure for extracting first command-line parameter (argv[1]) so that it returns the full path, not just the first letter?

I tried to put the path in quotes (myprogram.exe "D:\myfolder\myfile"), but it didn't help.

Community
  • 1
  • 1
Glory to Russia
  • 17,289
  • 56
  • 182
  • 325
  • 3
    Did you try between simple quotes ? Or escaping the backslashes (`myprogram.exe "D:\\myfolder\\myfile"`) ? – tomahh Oct 24 '12 at 10:55
  • It's not the quotes. `argv` is a character array, not a string array. You are explicitly selecting the item at index 1 of that array, which is the single character `D`. – Dan Puzey Oct 24 '12 at 10:57
  • Just wondering. In console applications, I've always used `main` (rather than `_tmain`), and I've never had any problems. – James Kanze Oct 24 '12 at 10:58
  • @DanPuzey `argv` is an array of pointers to C-style strings. If this is not what `_tmain` actually receives a `char*` or a `char[]`, then his code would crash, not just display the first character.\ – James Kanze Oct 24 '12 at 10:59
  • I tried it now. Escaping backslashes didn't help. If I put the path in simple quotes (`myprogram.exe 'D:\\myfolder\\myfile'`), then `Path: '` is displayed. – Glory to Russia Oct 24 '12 at 11:00
  • 2
    @Mat Having read the response you cite, I come to the conclusion that you should _never_ use `_tmain`, because it cannot work. Use either `main`, or if you're willing to be Microsoft specific, `wmain`. – James Kanze Oct 24 '12 at 11:02
  • @DanPuzey So how do I retrieve the first string in the array of strings (in this particular case) ? – Glory to Russia Oct 24 '12 at 11:03
  • @DmitriPisarenko: ignore me; James Kanze rightly pointed out I am talking rubbish :] – Dan Puzey Oct 24 '12 at 11:03
  • Shouldn't `_tmain` take a `TCHAR *argv[]` parameter anyway? Not that changing that alone will help you. – Steve Jessop Oct 24 '12 at 11:09

3 Answers3

6

Use _tprintf instead of printf. Your program is almost certainly set to compile as UNICODE and, therefore, each "character" takes up two bytes.

You call printf which operates on single byte characters. The second byte happens to be 0, and so printf thinks it reached the end of the string since a zero byte is considered the termination of a string.

And make sure your argv is declared as TCHAR and not char. Be consistent: either use the TCHAR routines, or don't. But don't mix unless you have a very good reason (i.e. you know what you're doing and you are gearing up to do some magic). Chances are you don't and you shouldn't.

Nik Bougalis
  • 10,495
  • 1
  • 21
  • 37
2

Visual Studio C++ has a special way of dealing with Unicode support. Macros in tchar.h expand to normal or wide char versions of the symbols depending on the project setting. So _tprintf expands to printf or wprintf. It seems like you're trying to use printf, if your project settings enable the Unicode support, what you're running into is the exact problem to expect. Have a look at tchar.h and try using _tprintf instead.

cyco130
  • 4,654
  • 25
  • 34
0

In visual studio the main function usually takes up the arguments of type wchar. If that is the case try outputting via the wprintf function as:

wprintf("Path : %S", argv[1]);

Notice the capital 'S'. If you substitute this with a lowercase 's' it will print the first character only. That is how the wide characters work. you should read more about them on the msdn forum.

Extn3389
  • 48
  • 8
  • 2
    No - it depends on the compilation and project settings. If he's using _tmain he should be using the tchar routines everywhere, consistently, unless there's a good reason (e.g. parsing and displaying non-UNICODE input from UNICODE app or vice-versa) – Nik Bougalis Oct 24 '12 at 11:08