0

I normally use 'getopt' for command line argument handling in Linux but sadly i come to know just now that it does not work in Visual Studio. Since this is my first time coding visual C++ 2010 code so i am having trouble parsing command line arguments. Pls see code below:

#include "StdAfx.h"
#include <iostream>
#include <string>


int _tmain(int argc, _TCHAR* argv[])
{
    std::string cfgFilePath = "";
    if(argc < 2)
    {
        std::cout<<"Usage: ApplicationName ConfigFile\n";
        exit(0);
    }
    //cfgFilePath = argv[1];    //<<--- Ideally i want to do this. But it doesnt work

    printf("\nEntered String: %s.", argv[1]);

    return 0;
}

When i run its exe like "app.exe abcd" then the output i get is "Entered String: a.". Why is this happening and what i can do to correct things.

Also what can i do to make commented out line to work. Here i am getting error

"*error C2679: binary '=' : no operator found which takes a right-hand operand of type '_TCHAR ' (or there is no acceptable conversion)"

Thanks

suspectus
  • 16,548
  • 8
  • 49
  • 57
HokageSama
  • 301
  • 1
  • 3
  • 11
  • "Entered String: a": Are you sure that you call the program with "app.exe abcd"? At least, i can say that even in VS and Windows, the described behaviour is not intended. There have to be error somewhere here. – deviantfan Jan 24 '14 at 17:53
  • Yes. I do call as app.exe abcd. I followed sample s in msdn and was hoping to get a complete string. Don't know why is this the case. – HokageSama Jan 24 '14 at 17:57
  • 2
    Is this compiled for Unicode? – OldProgrammer Jan 24 '14 at 17:57
  • 1
    @HokageSama _TCHAR is a macro that is controlled by the `UNICODE` and `_UNICODE` macros, you need to use an `std::wstring` – Mgetz Jan 24 '14 at 17:58
  • @Mgetz it gives an error stating "error C2440: 'initializing' : cannot convert from 'const char [1]' to 'std::basic_string<_Elem,_Traits,_Ax>'" – HokageSama Jan 24 '14 at 18:00
  • @OldProgrammer I suppose Unicode would be enabled in my visual studio. I will search options to turn it off and give it a try. – HokageSama Jan 24 '14 at 18:02
  • Well, turning off Unicode from project properties made commented part work. Also in this case i was able to get complete string. Thanks.. – HokageSama Jan 24 '14 at 18:05

0 Answers0