I am building a web application / Interface for my C++ program which will be hosted on the server and then using a scripting language ("PHP") I will then execute the program to run.
I am using G++ to compile the code and and I execute the command to run like so: ("./main") now is it possible that I can pass in the file location so then my program can run? So for example like this:
int main(int argc, char *argv[], string* fileLoc)
{
// code
}
Then execute like this ("./main(FILE_LOCATION)")?
Hope someone can help
You should keep the standard main signature int main(int argc, char *argv[]). The filename would be in argv[1], provided you execute it like this:
./main somefilename.txt
//main.cpp
#include <iostream>
int main(int argc, char* argv[])
{
if (argc > 1)
std::cout << argv[1] << "\n";
}
./main Hello_there
Hello_there
Related
When I run my C++ program I need it to open a text file stored in my root directory. How can I make CMake to execute the program I have written with the text file?
When I build my program with Makefile alone, I use the command
./"executable" src/"txt file"
Honestly, by far the simplest would be to just modify your main function. As it stands you main function must be grabbing the filename from the command line arguments. Something like this:
#include <iostream>
int main(int argc, char *argv[]) {
const auto filename = argv[1];
// Do stuff with filename
std::cout << filename;
return 0;
}
What you probably should do is to just modify that file to use some default filename when no argument is provided:
#include <iostream>
int main(int argc, char *argv[]) {
const auto filename = argc < 2 ? "/root/something.txt" : argv[1];
// Do stuff with filename
std::cout << filename;
return 0;
}
Depending on how complicated you want to get, you could also let that filename be specified in your CMakeLists. Just add a definition like
set(DEFAULT_FILENAME "/root/something.txt")
target_compile_definitions(my_target PRIVATE "DEFAULT_FILENAME=\"${DEFAULT_FILENAME}\"")
and then take the filename like a macro in your main function:
#include <iostream>
int main(int argc, char *argv[]) {
const auto filename = argc < 2 ? DEFAULT_FILENAME : argv[1];
// Do stuff with filename
std::cout << filename;
return 0;
}
To summarize... It sounds like you want to have one executable to be built with the filename into it. The aforementioned approaches will accomplish that.
However, there is a much simpler solution... just create a script that runs the file you want. For example, just throw your code
./"executable" src/"txt file"
into a script run.sh
Then make that script runnable (assuming Linux) with
chmod +x run.sh
and just run that script:
./run.sh
I search a lot but don't find nothing.
I want make a C++ software to run a software with argument in C++
Example : start putty -ssh user#server -pw password
start notepad -someargument
To start a different software than your own program (with or without arguments) you can use system() from <cstdlib> header.
#include <cstdlib>
int main(int argc, char* argv[]) {
system("start putty -ssh user#server -pw password");
return 0;
}
If you want to evaluate the arguments to your own program, you can use argv[]. argv[0] holds the name/path of your program, and argv[1] ... argv[argc-1] the actual arguments i.e.
#include <cstring>
#include <iostream>
int main(int argc, char* argv[]) {
if ((argc > 1) && (!strcmp(argv[1], "-help"))) {
std::cout << "Showing help" << std::endl;
}
return 0;
}
First thing,sorry my bad english and any mistakes in asking.
I've searched it a lot,but i was not able to explain in simple words.
I work with Linux servers and command line, i'm used to calling programs through it like
./program foo -u adm -p 123
But i always wondered how make programs to act like that,i mean call a specific function and write parameters without needing to open program itself.
In other words.
If i code a C++ like that,and compile
#include <iostream>
using namespace std;
void SayHello(string Name)
{
cout << " Hello " << Name;
}
how can i call it through the command line like
./Program SayHello CARLOS
Sorry about my ignorance,but it's something that i want to learn.
Thanks for your attention
If you want to call a function of your program based on the arguments, you could do something like:
int main(int argc, char* argv[]) {
if(argc > 2){
if(strcmp(argv[1], "SayHello") == 0){
SayHello(argv[2]);
}
}
return 0;
}
Of course this is just a sketch and i can be improved if what you want to achieve is more complex.
You could also build a more dynamic solution if you want other functions than the "SayHello" one to be callable too.
int main( int argc, char** argv )
Here argc refers to the number of argument (arg count)
argv refers to the argument array ("char*" array) (arg value)
Calling your program from command line will result in a main entry with these parameters ; it remains to parse them and launch the command accordingly.
void main() {
char *name[] = {
"./program",
"-c",
"foo -u adm -p 123",
NULL
};
execvp(name[0], name);
}
There you go every executable needs a main function which is the entry point for execution.
#include <iostream>
#include <string>
using namespace std;
void SayHello(string Name)
{
cout << " Hello " << Name;
}
int main(int argc, char* argv[])
{
if (argc > 1)
SayHello(argv[1]);
}
To compile this do
$g++ hello.cpp
It should produce a.out on Linux.
To run it
./a.out "World!"
I made an Xcode project. Like this:
Mac OS X -> Application -> Command Line Tool
I chose C++
And then I saved it in a folder.
Now I opened it in Xcode, there is a file named main.cpp under the project name title, so I opened that.
So main.cpp contains (location: Desktop/CPP/learn/learn/main.cpp)
#include <iostream>
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
return 0;
}
Now I click on File > New File and I selected C++ file without header, I named it main1.cpp
So that main1.cpp now just has
#include <stdio.h>
I replaced that and made main1.cpp to
#include <iostream>
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "MAIN1DOTCPP!\n";
return 0;
}
Then I ran main1.cpp, And the out put was "Hello World", I have been trying to get the output of the second file, That is "MAIN1DOTCPP" for like 2 hours now, Can someone please help?
I installed codellite 7.0 yesterday and have been trying to work with it. But there seem to be some problem. I cannot run any code.
For now code is pretty simple.
#include <stdio.h>
int main(int argc, char **argv)
{
printf("hello world\n");
return 0;
}
however, it returns following and output is blank with Press any key to continue
Current working directory: D:\ .....
Running program: le_exec.exe ./1
Program exited with return code: 0
Your program is running fine, the only problem is that after the printf the program returns 0 and shuts down immediately, it does run and print out "hello world", you just don't have the time to see it.
To make the program wait for user input so that you can see the output use cin.get() :
#include <stdio.h>
#include <iostream>
int main(int argc, char **argv)
{
printf("hello world\n");
std::cin.get();
return 0;
}