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/
Related
I'm working on a traveling sales person program in C++. I'm very new to C++ and its so much different from Java that the simple things really get confusing.
How do I use standard input to accept commands from a file (apparently I don't open the file, my professor just uses it to feed in commands or something) and then use those commands to run my program accordingly?
An example would be
city a
city b
city c
city d
a c 1300
a d 1400
a b 900
d c 1500
So basically an unknown amount of information is going to be passed into my program and then my program needs to declare a specific number of cities and then attach travel costs between each of them as specified. I think I can do the latter part, but my problem is knowing how to take an unknown number of inputs and then attach those inputs to variables.
I guess in Java I would do something like this:
While(nextLine = true){
if (nextLine.contains ("city"){
String nextLine = nextLine;
...and so on
}
}
Start with waiting a filename by ifstream, then you can get inputs by char or line in which using a char pointer and determine it with text size i believe somethig like this
std::ifstream::pos_type filesize(const char* filename)
{
Now you buffered, go on for what you know from java and combine it. Besides, like Sam's sugesstion, u you should read
Let me specify the question. I want to make commands so when user will type eg. go (something) it will use the go to specify the function and something to find what the program should do when variable equals something. If there is anything unclear, just ask, i know my explanation is strange.
Example:
In program there is a void go(string choice) function with if condition including few variables that can be used through choice string.
User is opening program and typing go and then string. Program goes to function go and if string is included in if loop, then program goes to specific if condition and does what it should eg.:
if(choice == "room")
{
//condition being executed
}
So basically you want a specific function to be executed according to a cmd line input? Easy! Create you main function like this:
int main(int argc, char *argv[])
argc is the argument count and argv is the argument value. Google up on this if you need more info, it's widely used. Use an if statement inside your main function that will call the required function according to the value of argv[]. So when you execute your program you'll execute like
exe_file_name go anything
If you want to call multiple functions dynamically during a single execution you could instead do
getline(cin,choice);
if(strcmp(choice, "room")) //condition
I think it should be like
if(strcmp(choice , "room")){//condition being executed}
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.
Actually I have trouble naming the title of this post. Because I don't know how to summarize my meaning in a professional way. But I'll explain my question as below:
I'm running a program written by C++, command is:
./VariationHunter_SC
Then it'll let you type in many parameters:
Please enter the minimum paired-end insert size:
Please enter the maximum paired-end insert size:
Please enter the pre-processing mapping prune probability:
Please enter the name of the input file:
Please enter the minimum support for a cluster:
Obviously I need to type in such parameters one by one to run the program; But I have thousands of such jobs, and need to pre-assign such parameters in script, and submit script to computer.
So how can I do that?
thx
Edit
so how can I make parameter-list?
Just like below?:
140
160
0
mrfast.vh
1
Seems the program cannot recognize these numbers, and distribute numbers..
This depends on how the program actually reads the data that you type in - it's likely that its reading stdin, so you could use separate files with the parameters and pass them in via redirection: ./VariationHunter_SC < parameter-file
It's also possible that the program will accept parameters on the command line, but there's no way of really knowing that (or how) except by whatever documentation the program might come with (or by reading the source code, if it's available and there is no other accurate docs).
Simply use the piping character to pipe the contents of a file to your program
example, in a windows command shell:
echo "asdf" | pause
This will pass "asdf" to the pause program. As a result, pause will print a "Press any key to continue" message, then imediately continue because it will receive the "asdf" string as a response.
So, overall, write or use a program that outputs the contents of your file. Call it, then pipe its output to the program that needs the input.
The unix cat command is such a command that writes the contents of a file to output, or to the input of another executable if you are piping the output.
I have a c++ program which performs one function. It loads a large data-file into an array, receives an array of integers and performs a lookup in that array, returning a single integer. I am currently calling the program with each integer as an argument, like so:
$ ./myprogram 1 2 3 4 5 6 7
I also have a ruby script, and I would like this script to utilize the c++ program.
Currently, I am doing this like so.
Ruby Code:
arguments = "1 2 3 4 5 6 7"
an_integer = %x{ ./myprogram #{arguemnts} }
puts "The program returned #{an_integer}" #=> The program returned 2283
This is all working properly, but my problem is that each time ruby makes this call, the c++ program has to reload the data-file (which is over 100mb) - very slow, and very inefficient.
How can I rewrite my c++ program load the file only once, allowing me to make many lookups via a ruby script without reloading the file each time. Would using sockets be a sensible approach? Writing the c++ program as a ruby extension?
Obviously I am not an experienced c++ programmer, so thanks for your help.
A possible approach is to modify your C++ program so that it takes its input from the standard input stream (std::cin) instead of from the command line parameters, and returns its result through the standard ouput (std::cout) instead of as main's return value. Your Ruby script would then use popen to launch the C++ program.
Assuming the C++ program currently looks like:
// *pseudo* code
int main(int argc, char* argv[])
{
large_data_file = expensive_operation();
std::vector<int> input = as_ints(argc, argv);
int result = make_the_computation(large_data_file, input);
return result;
}
It would be transformed into something like:
// *pseudo* code
int main(int argc, char* argv[])
{
large_data_file = expensive_operation();
std::string input_line;
// Read a line from standard input
while(std:::getline(std::cin, input_line)){
std::vector<int> input = tokenize_as_ints(input_line);
int result = make_the_computation(large_data_file, input);
//Write result on standard output
std::cout << result << std::endl;
}
return 0;
}
And the Ruby script would look like
io = IO.popen("./myprogram", "rw")
while i_have_stuff_to_compute
arguments = get_arguments()
# Write arguments on the program's input stream
IO.puts(arguments)
# Read reply from the program's output stream
result = IO.readline().to_i();
end
io.close()
Well,
You could go about this a number of different ways.
1) A simple, potentially ugly way to do this is to have your c++ run and intermittently check for a file, have your ruby script produce said file containing your arguments. Your C++ program would then use the contained arguments returning it's result to a result file which you could wait on within your ruby script... This is obviously HACK TASTIC but it's uber simple to implement and would work.
2) Expose your c++ code as a c extension to ruby. This is not as hard as it's sounds especially if you use RICE and would provide significantly less hackie solution.
3) If your c++ can be exposed through a c header file then it's almost trivial to construct a bridge using FFI. Jeremy Hinegardner gave a good lecture on constructing FFI interfaces at rubyconf heres the screencast
4) D-Bus provides an application communication bus, you could alter your C++ app to take advantage of said event bus and pass messages from your ruby using ruby-dbus
There are of course a thousand other routes... Maybe one or the other of these could prove viable :)
Cheers!