How to access argv strings?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: How to access argv strings?

  1. #1
    Join Date
    Dec 2012
    Posts
    13

    How to access argv strings?

    I have:

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

    It displays only the first character of the first argument on the command line.

    Yet the following works as expected:

    static char* myArg[2] = {"first arg","second arg"};
    printf("drive name: %s\n", myArgv[1]);

    That displays the entire string "second arg".

    So what's wrong with my reference to argv[1]?

  2. #2
    Join Date
    Dec 2012
    Posts
    13

    Re: How to access argv strings?

    Quote Originally Posted by joeu2004 View Post
    I have:
    int _tmain(int argc, char* argv[])
    {
    printf("drive name: %s\n", argv[1]);
    It displays only the first character of the first argument on the command line.
    [....]
    So what's wrong with my reference to argv[1]?
    Okay....

    The original declaration was _TCHAR* argv[], which declares argv[] as pointer to strings of wide chars (wchar_t).

    And sure enough, when I revert to that declaration, printf("%ls",argv[1]) displays the entire strings. Note the use of "%ls" instead of "%s".

    So I guess my question is: how can I coerce VC++ to pass normal chars (char* argv[]) instead of wide chars?

    Probably some kind of build option. If that is the case, I would appreciate click-by-click instructions. I'm a little rusty with using VC++.

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,097

    Re: How to access argv strings?

    Quote Originally Posted by joeu2004 View Post
    Okay....

    The original declaration was _TCHAR* argv[], which declares argv[] as pointer to strings of wide chars (wchar_t).

    And sure enough, when I revert to that declaration, printf("%ls",argv[1]) displays the entire strings. Note the use of "%ls" instead of "%s".

    So I guess my question is: how can I coerce VC++ to pass normal chars (char* argv[]) instead of wide chars?

    Probably some kind of build option. If that is the case, I would appreciate click-by-click instructions. I'm a little rusty with using VC++.
    It's in the project properties. Under Configuration Properties/General/Character Set, select Multi-bye character set.

  4. #4
    Join Date
    Dec 2012
    Posts
    13

    Re: How to access argv strings?

    Quote Originally Posted by GCDEF View Post
    It's in the project properties. Under Configuration Properties/General/Character Set, select Multi-bye character set.
    Great! Thanks. I wanted "Not Set".

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,977

    Re: How to access argv strings?

    To build with any setting:
    Code:
    int _tmain(int argc, TCHAR* argv[])
    {
        _tprintf(TEXT("drive name: %s\n"), argv[1]);
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center