C++ What is my own file name? - c++

I have a piece of C++ code that calls the system command.. I want to pass the file name of my own C++ executable to the system command.. anyone know how to do this?
So for example my C++ code is called "switch-5".. what I want to do is something like;
system("./script.sh switch-5");
Anyone have any clue?

Your executable name is the first argument passed in argv.
To test this just run:
int main(int argc, char **argv)
{
printf("My program name: '%s'\n", argv[0]);
return 0;
}
(I am assuming you know how to combine it with your script name to get the string to pass to system().)

argv[0] as passed to main is the name of your executable.

Store it in a global variable during app startup:
static char *selfname;
int main(int argc, char **argv)
{
selfname = argv[0];
// etc.
}

Beware, argv[0] contains exactly what was used to start the program, including relative or absolute path, for example: "./programname".

Related

Application is unable to get command argument

I'm unable to get command line argument with int main(int argc, char* argv[]), No errors occurs but when further manipulation want to happen i see that argv[ ] and other related variables dont have any value and says Error reading characters of string then an Acces violation reading location error happens.
This is the sample code of my issue, I had to downsize it to make it readable:
#include "CommonHeaders.h"
void Start(char *input)
{
lstrcpyA(host, input);
// In this point i see in my Debugger "Locals/Autos" that nothing
// is passed to function then a "Access violation ... " happens.
// ...
}
int main(int argc, char *argv[])
{
Start(argv[1]);
return 0;
}
I always use this int main(int argc, char* argv[]) and pass command arg with ProjectProperties->Debugging->Command Argument and works perfect everytime. Is it possible that proper headers aren't included or any changes in project configuration could make a conflict?
Be sure to set the subsystem to console Linker -> SubSytem -> Console (/SUBSYSTEM:CONSOLE) and dont set an Entry point set the Whole Program Optimization to No Whole Program Optimization and turn SDL check to off.

Objective-C variables read as null in C++

I'm writing a cross-platform application and need to pass across argc and argv from Objective-C in Xcode to my generic argument handler class in C++. I have a global pointer to this handler class which I set with a new command, but because I can't do new in Objective-C I'm trying the following:
I have a header file called MacCommandLineArgs.h which contains only the following:
static int cmdlArgc = 0;
static const char** cmdlArgv = NULL;
I then set these from within main.m:
int main(int argc, char *argv[])
{
cmdlArgc = argc;
cmdlArgv = (const char**)argv;
return NSApplicationMain(argc, (const char **)argv);
}
Once the Objective-C++ section of the application has been entered, I try to read back these global variables in order to pass them to the pure-C++ class:
int argc = cmdlArgc;
const char** argv = cmdlArgv;
globalCommandLineArgs = new CCommandLineArgs(argc, argv);
When stepping through with the debugger, cmdlArgc and cmdlArgv show up as valid data but argc and argv are still 0 and NULL after the assignment. What am I doing wrong here?
You don't show how cmdlArgc and cmdlArgv are declared; it's surely in a header file, but what does it look like?.
Nothing outside of main.m will have access to those variables as they have been defined static, which leads me to wonder why you aren't getting linker errors. I have concluded that the declaration of the of cmdlArgc and cmdArgv is something like this:
int cmdlArgc;
const char** cmdlArgv;
rather than this:
extern int cmdlArgc;
extern const char** cmdlArgv;
So every implementation file that includes the header will get their own copy, which is why it's 0/NULL.
The solution is to drop the use of static in main.m and start using extern in the header.
However it's still an ugly pattern and I would think the very best solution is to rename main.m to main.mm and initialise CCommandLineArgs in main(), which is both clean and simple.

How to open a file which has name with unicode symbols

I created an .exe file and associated .myFile extension to that .exe file. I want to double click on any .myFile file and get that file opened by the .exe. For that I have done the following:
int main(int argc, char *argv[])
{
QString fileName(QObject::tr(argv[1]));
if ( fileName != "" )
{
mainWin.loadFile(fileName);
}
..................
}
But when I have named my file in unicode characters (e.g. "Здравствуй.myFile"), the instead of "Здравствуй" you can see "????????". How to solve this problem? I know this is solved problem because, for example, MS Word does that.
The previous answers that focus on int main(int argc, char** argv) are needlessly complex. Qt has a better alternative.
From the Qt documentation: On Windows, the QApplication::arguments() are not built from the contents of argv/argc, as the content does not support Unicode. Instead, the arguments() are constructed from the return value of GetCommandLine().
So, the correct answer is to use qApp.arguments().at(1), which will give you the Unicode filename in a QString.
You have to use wmain instead of main on Windows:
int wmain(int argc, wchar_t** argv) {
QString fileName = QString::fromWCharArray(argv[1]); // untested
If you have to follow the C standard (which is all but useless on Windows), you can use GetCommandLineW and CommandLineToArgvW instead.
Assuming the unicode you pass in is actually stored as UTF-8, try using QString::fromUtf8 to read the argument, something like this:
int main(int argc, char *argv[])
{
QString fileName(QObject::trUtf8(argv[1]));
if ( fileName != "" )
{
mainWin.loadFile(fileName);
}
// stuff
}
QObject::trUtf8 is actually a wrapper that will utilize QString:fromUtf8 and still perform the translation (even though i dont understand why you want to translate file names)

executable parameter c++

if i got a c++ executable file like this: executable.exe and i want to add some parameters like:
executable.exe +username = pino
how do i get in c++ that i filled in pino as my username?
Arguments to main. Your main is
int main(int argc, char **argv) {
...
}
All the command-line params are in argv. There are also Windows-specific APIs.

Printing all environment variables in C / C++

How do I get the list of all environment variables in C and/or C++?
I know that getenv can be used to read an environment variable, but how do I list them all?
The environment variables are made available to main() as the envp argument - a null terminated array of strings:
int main(int argc, char **argv, char **envp)
{
for (char **env = envp; *env != 0; env++)
{
char *thisEnv = *env;
printf("%s\n", thisEnv);
}
return 0;
}
#include <stdio.h>
extern char **environ;
int main() {
char **s = environ;
for (; *s; s++) {
printf("%s\n", *s);
}
return 0;
}
I think you should check environ. Use "man environ".
Your compiler may provide non-standard extensions to the main function that provides additional environment variable information. The MS compiler and most flavours of Unix have this version of main:
int main (int argc, char **argv, char **envp)
where the third parameter is the environment variable information - use a debugger to see what the format is - probably a null terminated list of string pointers.
LPTCH WINAPI GetEnvironmentStrings(void);
http://msdn.microsoft.com/en-us/library/ms683187%28VS.85%29.aspx
EDIT: only works on windows.
int main(int argc, char **argv, char** env) {
while (*env)
printf("%s\n", *env++);
return 0;
}
int main(int argc, char* argv[], char* envp[]) {
// loop through envp to get all environments as "NAME=val" until you hit NULL.
}
In most environments you can declare your main as:
main(int argc,char* argv[], char** envp)
envp contains all environment strings.
If you're running on a Windows operating system then you can also call GetEnvironmentStrings() which returns a block of null terminated strings.
Most of the answers here point out the possibility to pick the environment from an argument to main supported by most compilers. While Alex' answer:
#include <stdio.h>
extern char **environ;
int main() {
char **s = environ;
for (; *s; s++) {
printf("%s\n", *s);
}
return 0;
}
should work always, I wonder what happens to char **environ when you manipulate the environment in your C code (putenv, unsetenv). Then environ may point to somewhere else (when it was reallocated, may depend on the system implementation). If we stick to a parameter passed to main and pass it on to the function requiring it, this pointer may not point to the current environment any more.
More or less portable C code solution seems for me as follows:
#include <stdlib.h>
void printenv() {
char ** env;
#if defined(WIN) && (_MSC_VER >= 1900)
env = *__p__environ();
#else
extern char ** environ;
env = environ;
#endif
for (env; *env; ++env) {
printf("%s\n", *env);
}
}
Explanations:
Tested successfully on Linux, Windows, Solaris, AIX.
Tested successfully on new versions of Visual Studio as well. The point is that since at least VS 2017 (probably earlier) the environ global variable is not recognized anymore. If you open the header C:\Program Files\Windows Kits\10\Include\x.x.x.x\ucrt\stdlib.h you will see that this global variable was replaced with the function __p__environ(). Unfortunately it is not documented well. No word about it in https://learn.microsoft.com/en-us/cpp/c-runtime-library/environ-wenviron?view=msvc-170.
The advantage of this approach is that it is also appropriate if you are not allowed to modify the main() function adding there envp[].
Regarding GetEnvironmentStrings(), it returns me an empty list. Probably it works for C++ and not for C. I did not investigate it.