C++ cin cout to another exe - c++

I have 2 different executables which use traditional cin/cout to interact with the user.
Lets imagine it is a program that inputs some numbers and outputs the result.
Now I want to write a driving program in C++ that calls these executables and provides inputs to them ("cin>>"'s a value) and also gets the result of their cout.
I tried using system() that makes a command line call that starts the executable. But I am not able to provide a further input(in some cases inputs) to that. Any idea how to do that?
Unfortunately I can't switch the program to take command line arguments.

Related

How to print out specific lines of user input to console (C++)

I am using c++ and the terminal. So my program takes in user input using read(STD_FILENO,buf,BUFFER and I am trying to write back only specific lines.
So for example, if the user entered in a total of 10 lines, how would I print out lines 3 through 7 or 6 through 10?
I am trying to use the write() function (write(STD_FILENO,buf,BUFFER)) but it's not printing what I want it to.
I have tried messing around with the BUFFER and tried to make it smaller than the total amount of characters that the user has input, but it is still not working.
My understanding is that whatever I say the BUFFER is to be, it will write UP TO that BUFFER value, so it will start from 0 to BUFFER. But if I wanted to start from line 6, that may start on character #15 and not 0...does this make sense?
please note: I need to use read() and write()
Thank You!
If you are required to only use read(2) and write(2), then you'll also need open(2), close(2), lseek(2) and you need to design and code your own buffered IO library above it. Read carefully the documentation of every system call mentioned here. Use the result of each of them. Handle error cases in your code. See errno(3) & perror(3).
So keep a buffer (or more than one) and several pointers (or offsets) into it (probably at least the currently consumed position, and the last read position, etc).
Perhaps you'll want to use some container. You might start implementing your own equivalent of fgetc on your buffered IO class, and build above that.
Lines do not really exist at the system call level. You need to take care of \n in your code.
BTW you could study, for inspiration, the source code of several free software C libraries implementing <stdio.h>, such as musl-libc
Of course you should compile with all warnings and debug info ( g++ -Wall -Wextra -g with GCC) and you'll need to use the debugger gdb to understand the behavior of your program and find your bugs. Don't be shy in drawing on some board what happens in your virtual address space (with pointers represented by arrows).
NB: SO is not a do-my-homework service.

Fortran and gnuplot: parameter passing and returning a value

I would like to call a gnuplot script from a program in Fortran. The program is supposed to perform a linear fit, to obtain the linear parameters and to send them back to the main program. I know that gnuplot can be called from Fortran using the command
call system ('gnuplot script.gnu')
what I don't know is how to send parameters to gnuplot in this call (let's say a real variable called t) and to return back to the main program the values of the fitted parameters (two real values a and b).
Note: I would like to avoid the use of files to communicate between the programs: I don't want gnuplot to write a file that it is then read by the Fortran program.
My script for the linear fitting:
f(x)=a*x+b
fit [t:*] f(x) "data.txt" u 1:2 via a,b;
You can use the -e command line argument of gnuplot
call system (`gnuplot -e "t=1" script.gnu')
to pass a parameter to gnuplot. I am not aware of any way to return a value though.
(To make it really useful you will have to get the numbers into the string, see
Convert integers to strings to create output filenames at run time and many related question in the "Linked" tab about how you can do that.)
It should be very easy to do your linear fit in Fortran, the formula is very simple and there are also libraries available, and avoid all these complications.

GetCommandLine linux *true* equivalent

Similar question to Linux equivalent of GetCommandLine and CommandLineToArgv
Is it possible to get the raw command line in linux? The file /proc/self/cmdline is destroyd.
./a.out files="file 1","file 2" param="2"
prints
./a.outfiles=file 1,file 2param=2
which is junk
Escaping command line does work for all arguments but the first.
./a.out files=\"fil 1\",\"fil 2\"\ param=\"2\"
prints
./a.outfiles="fil1","fil2" param="2"
You can't do that. The command line arguments are actually passed to the new process as individual strings. See the linux kernel source:
kernel_execve
Note that kernel_execve(...) takes a const char *argv[] - so there is no such thing as a long string commandline in Linux - it's the layer above that needs to split the arguments into separate components.
Edit: actually, the system call is here:
excve system call
But the statement above still applies. The parameter for argv is already split by the time the kernel gets it from the C-library call to exec.
It is the responsibility of the "starter of the program" (typically a shell, but doesn't have to be) to produce the argv[] array. It will do the "globbing" (expansion of wildcard filenames to the actual files that it matches) and stripping of quotations, variable replacement and so on.
I would also point out that although there are several variants of "exec" in the C library, there is only one way into the kernel. All variants end up in the execve system call that I linked to above. The other variants are simply because the caller may not fancy splitting arguments into invdividual elements, so the C library "helps out" by doing that for the programmer. Similarly for passing an environment array to the new program - if the programmer don't need specific environment, he/she can just call the variant that automatically take the parent process env.

Test environment for an Online Judge

I am planning to build an Online Judge on the lines of CodeChef, TechGig, etc. Initially, I will be accepting solutions only in C/C++.
Have thought through a security model for the same, but my concern as of now is how to model the execution and testing part.
Method 1
The method that seems to be more popular is to redirect standard input to the executable and redirect standard output to a file, for example:
./submission.exe < input.txt > output.txt
Then compare the output.txt file with some solution.txt file character by character and report the results.
Method 2
A second approach that I have seen is not to allow the users to write main(). Instead, write a function that accepts some arguments in the form of strings and set a global variable as the output. For example:
//This variable should be set before returning from submissionAlgorithm()
char * output;
void submissionAlgorithm(char * input1, char * input2)
{
//Write your code here.
}
At each step, and for a test case to be executed, the function submissionAlgorithm() is repeatedly called and the output variable is checked for results.
Form an initial analysis I found that Method 2 would not only be secure (I would prevent all read and write access to the filesystem from the submitted code), but also make the execution of test cases faster (maybe?) since the computations of test results would occur in memory.
I would like to know if there is any reason as to why Method 1 would be preferred over Method 2.
P.S: Of course, I would be hosting the online judge engine on a Linux Server.
Don't take this wrong, but you will need to look at security from a much higher perspective. The problem will not be the input and output being written to a file, and that should not affect performance too much either. But you will need to manage submisions that can actually take down your process (in the second case) or the whole system (with calls to the OS to write to disk, acquire too much memory....)
Disclaimer I am by no means a security expert.

Program to call other programs

I am writing a program that will solve a type of min. spanning tree problem. i have 2 different algorithms that I've gotten working in two separate .cpp files i've named kruskels.cpp and prims.cpp.
my question is this:
each file takes the following command line to run it . time ./FILENAME INPUTFILE FACTOR
i would like to make a program that, depending on what inputfile is entered, will run either kruskels.cpp or prims.cpp. how can i do this?
this program must pass those command line arguments to kruskels or prims. each file (kruskels.cpp and prims.cpp) are designed to be run using those command line arugments (so they take in INPUTFILE and FACTOR as variables to do file io).
this should be for c++.
You can call external programs using the system function.
However, it would be much better to build your Kruskal and Prim solvers in a modular way as classes, and instantiate the appropriate class from your main, according to the input. For this you'll link kruskels.cpp, prims.cpp and your main.cpp into a single executable.
The standard way is to use system(). You might also want to look up popen() (or, on Windows, _popen()).
Edit: My assumption was that you have two executables and (critical point) want to keep them as separate executables. In that case, using system is pretty straightforward. For example, you could do something like:
std::stringstream buffer;
if (use_Kruskals)
buffer << "Kruskals " << argv[1] << argv[2];
else
buffer << "Prims " << argv[1] << argv[2];
system(buffer.str().c_str());
Depending on what you're doing (and as Eli pointed out) you might want to create a single executable, with your implementations of Prim's and Kruskal's methods in that one executable instead. Without seeing your code for them, it's impossible to guess how much work this would be though.
If you need your top program to regain control after executing one of your two child programs, use system() or popen(), if you don't need it then you can use execve()