Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
myFunction(){
char *tempPath = getenv("LocalAppData");
strcat(tempPath, "\\MS\\namedPipe.json");
printf(" the path is %s \n",tempPath
}
int main(){
myFunction();
myFunction();
return 0;
}
I don't know the second time that I call this function I am getting the path to be appended like
Quoting the man page for getenv:
As typically implemented, getenv() returns a pointer to a string within the environment list. The caller must take care not to modify this string, since that would change the environment of the process.
In other words, what you are currently doing is not allowed.
Instead make another buffer and concatenate in that buffer. Eg:
char *tempPath = getenv("LocalAppData");
if (tempPath != NULL)
{
std::string env;
env = tempPath;
env += "\\MS\\namedPipe.json"
std::cout << env;
}
else
{
std::cout << "No such environment variable\n";
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
template<typename... Args>
void callJavaScript(const Args&... args) {
// TO-DO
}
callJavaScript({
console.log("Hello World")
})
Is it possible to accomplish something like this with Variadic templates (with/without Macro hack)?
Instead
callJavaScript("{
console.log('Hello World')
}")
I want
callJavaScript({
console.log("Hello World")
})
Here an example I found that use Macros,
#define MULTI_LINE_STRING(a) #a
const char *text = MULTI_LINE_STRING(
Using this trick(,) you don't need to use quotes.
Though newlines and multiple white spaces
will be replaced by a single whitespace.
);
Here is my version of this macro,
#define createScript(name, ...) \
const char *name = #__VA_ARGS__;
Answering my own question.
void callJavaScript(std::string script) {
std::cout << script << "\n";
}
#define callJavaScript(...) callJavaScript(#__VA_ARGS__)
Now you can call like this,
callJavaScript({
console.log("Hello World")
})
You can compile this then it will output
{console.log("Hello World")}
If anyone has a better way with templates, please do tell.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
This is the content inside a text file which has function call and if conditions.
File is written by following python syntax.
File.txt
move(1,2,3)
stop(1,4,5)
if(condition):
move(1,1,1)
if(condition2):
start(4,2,1)
elif(condition3):
stop(0,0,0)
elif(condition4):
move(10,0,0)
* .cpp
#include<iostream>
#include<fstream>
using namespace std;
int main() {
ifstream myReadFile;
myReadFile.open("file.txt");
char output[100];
if (myReadFile.is_open()) {
while (!myReadFile.eof()) {
myReadFile >> output;
cout<<output;
/*
inside here how can i take content from file and interpret and run it as normal c++ commands
*/
}
}
myReadFile.close();
return 0;
}
Now, after reading the content using c++ ,how to parse it and run the following commands inside c++.
I dont know the tags which i have given are correct, so do let me know proper tags
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am writing code C++ code in Xcode. At an instance I'm making sure that all fields are valid.
SomeClass *myclass = new SomeClass();
std::string myString;
if ( (myClass) && (myString.c_str)) {
return true;
} else {
return false;
}
Should i be checking for testString.c_str? Does it makes sense?
The default behavior of the new() operator is to either return the new object, or throw an exception if memory allocation failed. So, you don't need to check if myClass is NULL, unless you set the flags to change the behavior or implemented your own new() operator for your class.
Also, the extra brackets around myClass are not necessary. A better way to express what you want to check would be
if ((myClass != nullptr) &&
Then, you are currently testing if the address of the method c_str() in the std::string class is not NULL. Not want you want to do, I guess.
First, you would need to write myString.c_str(). Then, this method never returns a NULL pointer, what it can return is an empty C string. But this is better tested with std::string::empty(), so your check would look like this:
if (myString.empty()) {
return false;
} else {
return true;
}
which can of course be shortened into
return !myString.empty();
Finally: If you have this code in a function/method: Who deletes your new SomeClass object?
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am working on an object-oriented C++ coursework where I need to return error codes from the main function. How would one do this properly?
Unfortunately this is an assessed coursework so I cannot post my code here. But let's say the case is as follows:
I'm building an enigma machine with classes Plugboard, Reflector, and Rotor. I pass each of the configuration files as arguments in the command line. In this task, I'm provided with a file errors.h containing the following:
#define INSUFFICIENT_NUMBER_OF_PARAMETERS 1
#define INVALID_INPUT_CHARACTER 2
#define INVALID_INDEX 3
// and so on...
So I have in my program several functions to check the errors, for example a function to check whether the configuration file contains an invalid character (it has to be 0 to 25). I was thinking of setting this as a boolean function and then in my main function have the following:
if (!plugboard.check_invalid_character(/*some arguments*/)) {
cerr << "Invalid character!" << endl;
return 2;
}
But I'm not completely sure this is the right way to do it? Is it too superficial? Is there a more elegant way of returning error?
I hope my question is a little clearer this time. Thanks before.
You just need to return the value 4 in your main method like this:
int main() {
return 4;
}
Please note that your main function could also have the arguments vector and the argument count so there could be more in the brackets.
If KLibby is right and you use a method with returns the value you need to use something like that:
int doSomething() {
return 4;
}
int main() {
return doSomething();
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How do you call a function from a source file from a header file?
//h.h
extern string pic;
class takePic
{
public:
void warPic();
void artPic();
void fatePic();
void painPic();
void noPic();
};
// second part of the same header where it calls the function
takePic picture;
void pictureType()
{
if (pic == "war")
{
picture.warPic();
}
else if (pic == "fate")
{
picture.fatePic();
}
else if (pic == "pain")
{
picture.painPic();
}
else if (pic == "art")
{
picture.artPic();
}
else
{
picture.noPic();
}
}
When I do this it says that the linker is not working.
This is the error linker command failed with exit code 1.
What happens if you change
void pictureType()
to
inline void pictureType()
You should really tell us the whole error message, and perhaps try searching for that before asking a question.