I am trying to save argv into a vector as strings but I keep getting the error: see reference to function template instantiation 'std::vector<_Ty>::vector<_TCHAR*[]>(_Iter,_Iter)' being compiled
I have tried Save argv to vector or string and it does not work
I am using Microsoft Visual Studio 2010.
Here is my code:
#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<std::string> allArgs(argv + 1, argv + argc);
return 0;
}
The problem is that std::string doesn't have a constructor for the _TCHAR* type, so you can't generate a vector of strings from an array of _TCHAR*.
Try using as said by #NathanOliver the "normal" version of the main: int main(int argc, char *argv[]).
Or switch to std::wstring.
Note: If not compiling with unicode enabled, _TCHAR* may then be equivalent to char * and the code would not provoke any compiler errors.
Use this:
typedef std::basic_string<TCHAR> tstring;
// Or:
// using tstring = std::basic_string<TCHAR>;
// If you have latest compiler
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<tstring> allArgs(argv + 1, argv + argc);
return 0;
}
And if you would always want to use wide-strings, use this
int wmain(int argc, whar_t* argv[])
{
std::vector<std::wstring> allArgs(argv + 1, argv + argc);
return 0;
}
Or, if you want to have ANSI only (I'd not recommend), just use old style char and main.
Related
Is using the main function in the form:
int main(int argc, const char* argv[])
non-portable and non-standard? The C++ standard says that argv is "pointer to pointer to char". I prefer to use the word const to prevent modification and out-of-bounds in an unknown (to me) area of memory.
Is such a form wrong and non-portable?
Your version is indeed unportable.
The variants that the standard allows are
int main()
int main(int argc, char* argv[])
int main(int argc, char** argv)
The standard allows implementation-defined possibilities, so const might be permitted on some platforms. But if the implementation doesn't define such an alternative then the behaviour of your program is undefined.
So I am starting with c++ (i am trying to broaden my mind with new languages) but I came across a little issue which confuses me more than what I guess it should...
Using Visual Studio Express 2012, I created a console win32 application in C++ and this is my main method decleration:
// TestApp.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
however, since I dont know anything about c++, I searched for some tuts online and all of them had there declerations setup in a different manner
#include <iostream>
using namespace std;
int main()
{
cout<<"HEY, you, I'm alive! Oh, and Hello World!\n";
cin.get();
}
and
// my first program in C++
#include <iostream>
int main()
{
std::cout << "Hello World!";
}
I tried typing in the "std::cout", but it wouldnt accept it,
could someone just clarify why and the significance of the difference ?
The main method can be define with or without parameters. It all depends upon what you are using your application for.
Take a look at this: https://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fmainf.htm
Also for your program you need to have a return value
// my first program in C++
#include <iostream>
int main()
{
std::cout << "Hello World!";
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
is (at least I think so) a Windows only library and compiler depending way of declaring the main function.
Definitly not wrong is to declare main like this:
int main(int argc, char const *argv[])
{
//do something
return 0;
}
or like this:
int main()
{
//do something
return 0;
}
This is definitly proper C++ and you can use this universially.
C++ programs may have one of two beginnings:
int main(int argc, char *argv[])
or
int wmain(int argc, wchar_t *argv[])
The first of these gets its arguments (argv) as ANSI charachers, while the second gets "wide" characters -- generally UTF-16 or UTF-32, depending on the platform.
Microsoft define a framework to allow you make code that can compile with either ANSI or wide characters.
int _tmain(int argc, TCHAR *argv[])
Behind the scenes, they have something like this:
#if defined UNICODE
#define _tmain wmain
#define TCHAR wchar_t
#else
#define _tmain main
#define TCHAR char
#endif
They also have helper functions like _tprintf() and _tcscpy().
NOTE: as pointed out by others, the argc and argv params are optional, so you can also have
int main()
and
int wmain()
and (for Microsoft and compatible compilers)
int _tmain()
Also note that while _tmain() is not strictly portable, you can easily create your own #define macros if you want to be portable to other platforms.
I can't figure this one out. I have a c++ Application that works in Debug mode exactly as expected:
#include "stdafx.h"
#include <string>
#include <Windows.h>
#include <iostream>
using namespace std;
void truncateServer(std::string inString);
int _tmain(int argc, char *argv[])
{
char* server = argv[1];
truncateServer(server);
}
void truncateServer(std::string inString)
{
std::string server = "";
int whackCount = 0;
for (unsigned int i = 0; i < inString.length(); i++)
{
char c = inString[i];
if (whackCount < 3)
{
if (c == '\\') whackCount++;
else
server += c;
}
}
cout << server;
}
For example if I call the server I want via its UNC path \\serverName\Share\ in the debug it gives me exactly what I want: servername
However, if I use the release build I get nothing:
I deleted the release output folder, but the issue is exactly the same. I can only assume there is some other difference between the release and build applications that is exposing a major issue with my code? Or another difference between the outputs I need to account for.
What do I need to do to get the expected output?
It looks like your Debug build is set as Ansi and your release build as Unicode.
The _tmain declaration is a Visual Studio specific macro which changes the entry point of your application depending on the used charset.
For ANSI it maps to int main(int argc, char *argv[]) .
For Unicode it maps to int wmain(int argc, wchar_t *argv[]).
By using the char type as parameter to _tmain, you cause the compiler to use the wrong type when using an Unicode build, and so end up with a '\0' character as first byte, which std::string can't handle.
In your case, I recommend sticking to int main(int argc, char *argv[]) as it will work in all cases, especially with std::string which use chars.
Also, it is more portable across compilers and operating systems.
How can one take command line argument by not using below structure ?
int main ( int argc, char* argv ) {
}
My question is really : how can I take below input :
./executableProgramName inputX inputY inputZ inputT
in any function ?
in foo () {
// what should I write so that I can get same effect
}
Are there any other way for taking command line input ?
The method specified by the standard for getting command line arguments is the argc and argv parameters passed to the entry point function main. There's no other standard method.
Some platforms offer non-standard methods. For example, if you're on Windows you can use GetCommandLineW
Here's an example that uses some C++11 stuff too.
#include <ShellAPI.h> // for CommandLineToArgvW
#include <string>
#include <vector>
#include <codecvt>
#include <locale>
int main() {
#ifdef WIN32
LPWSTR *szArglist;
int argc;
szArglist = CommandLineToArgvW(GetCommandLineW(),&argc);
if(NULL==szArglist) {
std::cerr << "CommandLineToArgvW failed\n";
}
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,wchar_t> convert; // codecvt_utf8 or codecvt<char16_t,char,mbstate_t> should work (will work once the char16_t specialization of codecvt works)
vector<string> args;
for(int i=0;i<argc;++i) {
args.push_back(convert.to_bytes(szArglist[i]));
}
#endif //ifdef WIN32
}
Maybe the best way is to forward the handling of command line arguments into an object, or simply a function:
#include <vector>
#include <string>
void handle_commandline_args(const std::vector<std::string>& args){
...
}
int main(int argc, char* argv[]){
handle_commandline_args(std::vector<string>(argv[0], argv[0] + argc));
...
}
You can not arbitrarily get the command-line arguments from any function. In C++, the only way command-line arguments are passed in is through the char* array in the main function.
If you want them to be accessible from anywhere, consider keeping them in a global variable, or passing them into each necessary function call. For example:
int argumentCount;
char **argumentArray;
int main ( int argc, char** argv )
{
argumentCount = argc;
argumentArray = argv;
}
int foo()
{
std::cout << argumentArray[0]; // or whatever
}
If you're working with MSVC++, then you can use below win32 API to get command line arguments anytime in your program:
GetCommandLine
However, this makes your code non-standard. So it is better if you use main(int argc, char *argv[]) to get the command line arguments and save them for later use, e.g to be used by other functions.
Is there a way to get c++ strings from the commandline like in Java?
public static void main(String[] args)
where args is an array of C++ strings?
Not precisely but you can come close easily.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
typedef vector<string> CommandLineStringArgs;
int main(int argc, char *argv[])
{
CommandLineStringArgs cmdlineStringArgs(&argv[0], &argv[0 + argc]);
for (int i = 0; i < cmdlineStringArgs.size(); ++i)
{
cout << cmdlineStringArgs[i] << endl;
}
return 0;
}
This just uses the overloaded constructor for std::vector that takes a begining/ending iterator pair to copy the command line arguments into the vectors. It is much the same as java from there on.
You can likewise build and object around that vector with utility methods to convert arguments but there is almost no point. Also there are plenty of packages with object that deal with interpreting command line switches and such. ACE, POCO, QT, etc.. all come with such facilities.
You can use a vector to get the char array into strings.
#include <vector>
#include <string>
using namespace std;
int main (int argc, char** argv)
{
vector <string> args (argv, argv + argc);
}
Yes the main function can take 2 arguments
int main(int argc, char* argv[])
Where argc is the number of arguments and argv contains a list of the arguments.
For example if you run your program as:
MyProgram.exe hello
Then
argc = 2
argv[0] = MyProgram.exe
argv[1] = "hello"
Not built in to the language, but its very easy to implement:
#include <vector>
#include <string>
using namespace std;
int main( int argc, char *argv[] ) {
vector <string> args;
for ( int i = 0; i < argc; i++ ) {
args.push_back( argv[i] );
}
// do something with args
}
In C, your main function signature looks like this:
int main(int argc, char** argv).
argc contains the number of arguments passed in by the command line. The name of the executable is in position 0, and adds one to argc.
argv contains an array of strings containing the arguments. Again, position 0 is the name of the executable.
The standard C++ main() signature is
int main(int argc, char* argv[])
where argc denotes the count of commandline arguments and argv[] is an array of primitive C strings holding the commandline arguments. argv[0] is the executable, as invoked from the commandline.
If you are writing a win32 application, you can use GetCommandLineW http://msdn.microsoft.com/en-us/library/ms683156(VS.85).aspx and CommandLineToArgW http://msdn.microsoft.com/en-us/library/bb776391(VS.85).aspx
There is a comment on the CommandLineToArg page about special handling that is needed if your executable has spaces in the path and there are no arguments that you might have to handle.