This library support to load environment variables from a .env
file and read them like what .json
does but more simple!
- This project is owned by @harshfeudal. You can join my Discord server here!
- This library can be used for many different projects, currently, this library is enough to make a C++ Discord bot using D++ for safety token reader (you can use
.gitignore
to ignore.env
file).
- Download ONLY
dotenv
folder (make sure that it containsdotenv.h
inside). Simple right? - Include it in your project like this:
main.cpp
#include <dotenv/dotenv.h>
#include <iostream>
int main() {
// Load your .env file
dotenv::load(".env");
// Read your .env variable
char* my_var;
size_t envValueSize;
_dupenv_s(&my_var, &envValueSize, "MY_VAR");
// Read it
std::cout << "The variable is: " << my_var << std::endl;
// Remember to free it
free(my_var);
}
- Some notice update: You can also use
std::getenv("YOUR_ENV_VAR")
. However, some compiler like MSVC doesn't allow you to use that, and they recommend you to replace as_dupenv_s
. You can also use other methods if you find a new one! If you are not using MSVC, you can do like below (recommend):
#include <dotenv/dotenv.h>
#include <iostream>
int main() {
// Load your .env file
dotenv::load(".env");
// Read your .env variable
const char* my_var = std::getenv("MY_VAR");
// Read it
std::cout << "The variable is: " << my_var << std::endl;
}
... and compare it to your .env
file:
.env
MY_VAR="Hello World!"
Done! Simple right (I think)?
- Somehow you really want to skip line (or do whatever you want) like this:
The sentence is:
"Hello World"
- Hello 2023 -
... so, with this library feature, to do this, you can just do like this in your .env
file:
SENTENCE="\n\"Hello World\"\n\t- Hello 2023 -"
and it will work as expected!
- Or, you really want to make a comment like this:
TOKEN="WW91ckRpc2NvcmRCb3RUb2tlbg==" # Your Discord bot token
With this library features, your comment will be ignored after the #
mark! Comments begin where a #
exists, so if your value contains a #
please wrap it in quotes.
-
This library just include only a header file (
dotenv.h
)! -
Very similar with what Javascript does; The expansion engine roughly has the following rules:
$KEY
will expand any env with the nameKEY
${KEY}
will expand any env with the nameKEY
\$KEY
will escape the$KEY
rather than expand${KEY:-default}
will first attempt to expand any env with the nameKEY
. If not one, then it will return default
-
If you decide to make an empty variable like this:
EMPTY=
... so remember to have a space like this:
EMPTY= # A space after the '=' symbol
- Microsoft Windows 10
x64/x86
- Visual Studio platform
x64/x86
- C++ 17
ISO/IEC 14882
- Please leave me a star if you like it. Thank you very much!
- You can probably help me develop this project too by DMing me on my Discord profile.
I'd probably glad to see if you have anything new to help and support me. To do that, please make one and before you do that, just make sure you've tested the code. I won't bite you if you do it wrong, but just make sure that you have to test it clearly before I merge it.
You can download the latest version here.