I am trying to set an environment variable with the stdlib putenv() function on a windows computer.
Here is the code.
#include <stdio.h>
#include <stdlib.h>
extern char **environ;
int main(int argc, char **argv, char**envp){
putenv("MYNAME=Jamie"); // to set environment variable
for (int i=0; envp[i] != NULL; i++){
printf("%d: %s\n", i, environ[i]);
}
}
Upon compiling and executing the program, the environment variable doesn't appear in the list of printed env variables. Does anybody know why?
Related
I've been trying to call a program inside another c++ program using the command "execvp()" from the <unistd.h> library but it gets core dump, and i dont know what i am doing wrong;
below its the code i use to call the program i want to use
#include <iostream>
#include <unistd.h>
int main(int argc, char **argv)
{
char *argument_list[]={argv[1],argv[2],NULL};
char *filename = "./bin/program.exe";
execvp(filename, argument_list);
return 0;
}
below this, its the code of the program i want to call
#include <iostream>
int main(int argc, char **argv)
{
int a = atoi(argv[1]);
int b = atoi(argv[2]);
std::cout << a+b<<std::endl;
return 0;
}
when i compile the the first code, i get a "main.exe" binary, then i type "main.exe 5 6" to sum both integers, and i get the "core dump" error.
Curious thing is, if i run gdb on it, i get the sum i want
the first command line, its running directly the "child" program, showing that it works. The second command line, its using the "main" program that calls the child one
(Obviously, this programs aren't the ones i need to apply this, they're just for illustration of the problem, they're really big codes, and it wouldn't be helpful to post them here);
How can i fix this?
So, it works if i set the first argument as the filename, as said by #WhozCraig, so now it works, and looks like this:
#include
#include <unistd.h>
int main(int argc, char **argv)
{
char *argument_list[]={"program.exe",argv[1],argv[2],NULL};
char *filename = "./bin/program.exe";
execvp(filename, argument_list);
return 0;
}
However, i get this warning:
1
How can i get around it? Is there a problem leaving it like this?
Some of this code may seem foreign to you since I make 3ds homebrew programs for fun but it's essentially the same but with extra lines of code you can put in. I'm trying to read a file called about.txt in a separate folder. I made it work when I put it in the same folder but i lost that file and then my partner said he wanted it in Scratch3ds-master\assets\english\text and not in Scratch3ds-master\source I keep getting the error I coded in. I'm new to stack-overflow so this might be too much code but well here's the code:
#include <fstream>
#include <string>
#include <iostream>
int main()
{
// Initialize the services
gfxInitDefault();
consoleInit(GFX_TOP, NULL);
int version_major;
int version_minor;
int version_patch;
version_major = 0;
version_minor = 0;
version_patch = 2;
printf("This is the placeholder for Scratch3ds\n\n");
std::ifstream about_file;
about_file.open("../assets/english/text/about.txt");
if (about_file.fail())
{
std::cerr << "file has failed to load\n";
exit(1);
}
Chance are that you're using devkitpro packages. And chances are that the devkitpro team provide an equivalent of the NDS 'ARGV protocol' for 3DS programming. In which case, if you use
int main(int argc, char* argv[]);
you should have the full path to your executable in argv[0] if argc is non-zero.
https://devkitpro.org/wiki/Homebrew_Menu might help.
Your program has no a priori knowledge of what sort of arguments main() should receive, and in your question, you're using a main function that receives no argument at all.
Established standard for C/C++ programming is that main() will receive an array of constant C strings (typically named argv for arguments values) and the number of valid entries in that array (typically named argc for count). If you replace your original code with
#include <fstream>
#include <string>
#include <iostream>
int main(int argc, char* argv[])
{
// Initialize the services
// ... more code follows
then you're able to tell whether you received argument by testing argc > 0 and you'll be able to get these arguments values with argv[i].
With homebrew development, it is unlikely that you can pass arguments such as --force or --directory=/boot as on typical command-line tools, but there is one thing that is still useful: the very first entry in argv is supposed to be a full path for the running program. so you're welcome to try
std::cerr << ((argc > 0) ? argv[0] : "<no arguments>");
and see what you get.
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.