So I have a Windows Studio VS File named RectArea that contains the code to a function to find the area of a rectangle. In another Windows Studio VS file named main1, I have my main function like this:
int main(int argc, char *argv[])
{
return 0;
}
How do I use command line arguments to be able to print the area of the rectangle in the main file as soon as i compile it?
I read so much stuff about it online. I just still don't understand what I put for the executable and the stuff after it.
My function for the rectangle's area is in the RectArea file. Do I type in C://RectArea?
It's under a lot more folders though.
Ah, you're confused on how to pass the arguments to the program, not how to interpret them. Those arguments are not command line arguments passed to the Visual Studio build process; they're arguments that can be changed each time you run the program.
An executable written with an entry point like that can't be run properly via double-click like all the others you've written; you have to open cmd.exe and run the program from there, typing the arguments after the program name separated by a space.
Related
Issue
I have a Release-compiled MSVC C++17 "server" application. I usually make a child process from the client application to run a local server, so I use a relative directory path for the command line to execute as the child process. I am emulating that call here by typing in the relative path manually in powershell from where the client application executable runs.
It seems running run_Server.exe works with a command line like:
(Case 1)
PS C:\Users\me\Documents\MyProjectRelease1.0\Client_App_Folder> ../Server_App_Folder/run_Server.exe -help
(Proceeds to print out help)
But not with a command line like:
(Case 2)
PS C:\Users\me\Documents\MyProjectRelease1.0\Client_App_Folder\Client_App_Subfolder> ../../Server_App_Folder/run_Server.exe -help
(Waits a second then exits the program without any output)
Note: Client_App_Subfolder has an identical name as Client_App_Folder due to unimportant reasons.
Why is this happening? I may be mistaken, but I thought this did not happen under the exact same circumstances previously. I'm not finding much in the way of documentation about this phenomenon yet. I think this might have something to do with the up-directory ..\s in the command. Slash direction seems to not alter the results in this case in powershell.
Attempted Troubleshooting
Here's the first portion of main.cpp:
// Other #includes above.
#include <filesystem>
#include <iostream>
int main(int argc, char* argv[])
{
std::filesystem::path cwd = std::filesystem::current_path() / "filename.txt";
std::ofstream file(cwd.string());
file.close();
// ...proceed to parse command line arguments, etc.
}
This code results in outputting filename.txt in the MyProject\Client_App_Folder directory for the first case, but nothing for the second case.
One strange thing took my sleep away. .
I have P7 library. It is library for writing logs.
Library contains few examples.
Example for C++ looks like:
int main(int i_iArgC, char* i_pArgV[])
{
// Some code that don't use i_iArgC or i_pArgV
}
But the trick that program handle command line arguments somehow.
I play a little bit to make sure that this main called.
What I do:
Build in debug and set breakpoint on main (to make sure that exactly
this main is called)
Changemain(int i_iArgC, char* i_pArgV[]) to main() (To make sure that no one use them)
I have no idea how it possible.
Here is minimal steps you can do to look on it by yourself:
Download P7 code from this page (link at top left)
Unzip archive
Run build.sh (It runs few makefiles in some order)
Execute Cpp_Example from Binaries folder
Execute again Cpp_Example /P7.Help to see that app react to command line arguments.
Most systems allow for getting the command line parameters without relying on main(). On Windows for example, you can use GetCommandLineW().
The library has non-portable code to do just that in Shared/Platforms/*/PProcess.h. A quick look at Windows_x86/PProcess.h shows that it uses GetCommandLineW() and the same file in Linux_x86/ reads /proc/self/cmdline.
lets say we have a cal.exe file (a simple addition calculator programmed programmed in c++).
lets say that the console output screen first displays enter the first number: and waits for the user to input an integer value. I am willing to create a c++ program that would "pass on" the required value to the running process (cal.exe) as an input (playing the role of a user). I would also like to have the output from the cal.exe file to be displayed and interpreted by my program.
I havent got the slightest idea how to proceed with this. Is there any open source library that would help me accomplish this? If there is, could you name a few?
I have just learned object oriented programming in c++ last year in my school and I am not used to these kind of stuff in programming; so please excuse me if this question is silly.
update:
lets consider 2 processes a.exe and b.exe running. could you tell me a possible way to program b.exe which provides a integer input to a.exe (a console process) as if it was from the user?
You can do this by accepting command line arguments.something like this
int main ( int argc, char *argv[] )
{
enter code here
return 0;
}
Where,
First argument to main function (argc) refers to the number of arguments being passed to the program at run-time.
Second (char *argv[] )refers to a string containing the arguments that are passed (char * is treated as String also ).
Argument names may vary as per the user specifications.
For details Refer:
http://www.cplusplus.com/articles/DEN36Up4/
And for nesting of programs you can use system("name of child program goes here") Function under stdlib.h.
For details Refer:
http://www.cplusplus.com/reference/cstdlib/system/
In VS2010 I set command line arguments in the project settings->Debugging->Command line arguments:
-d 48000 1 -raw test1.opus test1_decoded.raw
However, when I debug the project and have a look at the argv[] in
int main(int argc, char *argv[])
{
}
... I can see that these command line arguments are missing.
The command line argument argv instead has only the path to the exe that is just being debugged. I see that if I move the mouse over the argv.
Does anybody have an idea what I might have done wrong?
Thank you for the help.
However, when I debug the project and have a look at the argv[]...
Per your description and code, I'm assuming that you are hovering your mouse over argv or looking at it in the watch window. argv is a pointer to pointer to char. The debugger does not know how many elements it contains. It will show you the first element i.e.,*argv`), but no more because there is simply no safe, standard way of doing so.
Your command line arguments are there, but the debugger cannot figure out how many elements to display in the UI. Look at the value of argc; that should match your number of arguments +1 for the path to your executable.
I have a C++ program that accepts three inputs: an integer for width, an integer for height, and a filename. Right now, I compile, and run the program like this (assuming I named it prog):
>prog
// hit enter
>128 128 output.ppm
This results in a successful output, but the program description says the proper command-line syntax is:
>prog w h filename
That's all it says. Does this imply that my program should be able to start on the same line? Perhaps it implicitly means you hit enter after typing the program name, but if not, is there a way to actually do this?
Your program needs to parse command line parameters. Looking at the specification, the expected workflow is
>prog 128 128 output.ppm
//hit enter after the parameters
Look here to learn more.
You're approaching the problem incorrectly. You are taking your input via std::cin after your program has been started. Your program specification states that the input should be given as part of the command. Consider a command like ls -l - the -l is part of the command and is passed to the program to parse and act upon.
You need to allow a command like prog 128 128 output.ppm to be run, so the user would type that and then press enter to run the program. How do you get access to the command line arguments within your C++ program? Well, that's what the argc and argv parameters of the main function are for. Your main function should look like this:
int main(int argc, char* argv[]) { ... }
The argc argument gives you the number of arguments passed in the command line (it will be 4, in the example given) which is also the size of the argv array. Each element is an argument from the command. For example, argv[0] will be "prog", argv[1] will be "128", and so on. You need to parse these values and, depending on their values, change the functionality of your program.
You can pass command via the argument in the main function:
int main(int argc, char *argv[]) {
}
argc is the number of arguments and argv is an array of arguments.