I have a two C++ source code in which one code generates an array for the specified input while other has to use array for execution. I want to know how can I link two C++ file such that output of the first file is the input for second one ?
Since they're separate programs, that means they each have a main() function. Because of that you can't link them together. What you can do, however, is use the shell to redirect the output from one program to the input of another. For example:
program1 | program2
The above creates a so-called "pipe". What it does is feed program2 with the output of program1. Only the standard input and standard output are redirected that way. In C++ that means std::cin and std::cout. Anything printed on std::cerr or std::clog is not redirected, so make sure to never print errors, warnings or other status/informational messages on std::cout. Only print the payload data and use std::cerr or std::clog for anything else.
Linux: Compile both files and push the content of the first to the second binary with a pipe in terminal else use a socket.. you can try to ouput the data with a binary-stream and the second binary can use the same techniqe to pushs it into a array.. i hope that helps you..
Related
I am very new to C++. I have my code and it displays my desired output in a win32 console.
However, my Instructor wants the output to be run through a .txt file. We have done this before with a program that had input already written within the coding.
ex.
cout << "example1....example2";
We achieved this with his exact instructions:
*1) Probably the easiest way to obtain a hard copy of the generated
program output on a Microsoft Windows platform is to run your
program from a command prompt, redirecting the output to a file.
The command line syntax would like like this:
lab1prog >lab1.txt*
My problem, however, is that I did this again for lab2 and redirected the output to lab2.txt but I need user input for this time around. When I run the lab2.exe file, my lab2.txt file outputs my "cout" statement and waits for input but I cannot enter input through a .txt file.
Please help if you can.
When you redirect the output of the program with
lab1prog >lab1.txt
then the user is still able to input data. The only problem is that she doesn't know when to enter what data. This is usually done by prompt outputs that are hidden whith the >lab1.txt.
To circumvent the problem you could abuse the error output what is not redirected with the command above.
cerr << "prompt";
To avoid this abuse you should use a "tee" program like wintee instead of redirecting with a simple >lab1.txt.
If it's acceptable to get the input from a file instead from the user you can use the input redirection.
labprog1 <input.txt >lab1.txt
If you specifically want to use a text file as input into your program, the easiest way (as Kamil Mikolajczyk mentioned) is to run it this way:
> lab1prog >lab1.txt <input.txt
On the other hand, if you need to run the program as it is, but have complete control over what to output into the file, I'd suggest using a file handle. Check out this question on how to use file handles. You could even duplicate the output on the command prompt and the file when you want by outputting it as:
fout << "My output";
cout << "My output";
You can as well output the user input into your output file for your convenience.
I have a program I am writing, I have already completed it, but the requirement/specification for it says that:
In the "Command Prompt", if someone runs your program like this:
Peter David < savednames.txt
it should print out the names inside savednames.txt that matches each of "Peter" and "David"
I have written the program but my own is interactive, i.e it asks for the file, then the name you want to search for, and then it prints the matches.
It works perfectly fine, but I don't understand what the running of the program in command prompt like this: "Peter David < savednames.txt" means. I am using C++ on Microsoft Windows.
Please I need your help to explain it and how to implement it in my code...is it some kind of operator overloading or ...I don't understand!
On the command line, < is used for input redirection. The shell opens the file whose name follows the < and copies its contents into standard input for the program.
So if you called program input.txt, you could open the file and read its contents using std::ifstream or whatever; if you called program < input.txt, you could just read the file's contents from stdin using cin.
You can also do the same for output. Instead of opening a file and writing to it in your code, write to stdout and call your program as program > output.txt.
yourprogramname Peter David < savednames.txt
means that your program will be called with Peter as first argument, David as second argument, and its standard input will be connected to a stream reading out of savednames.txt.
You just need to read standard input one row at a time, and process it according to the arguments you received.
You just need to modify the code you already have to take the names from the command line (is there a limit? Could there be three names? Or four? You need to consider that) and to read the data from standard input stream, without any need of opening a file.
How can I save all the input(cin) and output(cout, cerr) from a program whose input is taken from file(using "<")? I would like the input and output to be in order(so each input is followed by corresponding output as if I were typing the input in myself).
I tried ">" to output everything to a file, but that only saves standard output(no input/cerr), and just plainly copying the command line output still only gives the output without the input(because of how "<" works).
Is there a way to write everything(output+input) to file in order?
EDIT: edited for clarity
EDIT2: I just realized that it's impossible to do what I'm trying to do since the console does not know anything about when the commands would actually be entered. I'll have to manually enter commands and use the "script" command to actually log all input/output.
You need to add cerr to the stream
command > file 2&>1
This means put 2 (stderr) to 1 (stdout) as well.
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.
My program needs to accept three kinds of input commands below:
./Myprogram input.txt
./Myprogram < input.txt
./Myprogram
I'm thinking about using argc to check the number of arguments to resolve the first two situations (since redirection doesn't count as an argument). But then I stuck on the last case, which simply waits for an user input.
I'm wondering if there is a way to tell if a redirection is present in the shell command?
For a more complicated scenario such as a mix of redirection and argv forms (see below). Is there a way to do it or it's simply a bad design for taking user commands?
./Myprogram input1.txt input2.txt input3.txt
./Myprogram input1.txt < input2.txt input3.txt
./Myprogram
Any help will be much appreciated!
Z.Zen
Redirection will never be seen by your program as an argument. So in:
./Myprogram input.txt
./Myprogram < input.txt
./Myprogram
the second and third forms are identical. As for your second set of possibilities:
./Myprogram input1.txt input2.txt input3.txt
./Myprogram input1.txt < input2.txt input3.txt
./Myprogram
the second line is equivalent to:
./Myprogram input1.txt input3.txt < input2.txt
and it's also indistinguishable from:
./Myprogram input1.txt input3.txt
(the only different being where standard input actually comes from).
A typical way some programs handle mixed input from stdin and files specified on the command line is to accept "-" as a special filename meaning "use stdin as the input file at this position in the argument list". Many such programs will default to processing a singleton-list of "-" if the argument list is empty.
The basic algorithm is:
if (there are no arguments left after parsing options)
call_function(stdin);
else
{
foreach remaining argument
{
FILE *fp;
if (strcmp(argument, "-") == 0)
call_function(stdin);
else if ((fp = fopen(argument, "r")) == 0)
...error handling...
else
{
call_function(fp);
fclose(fp);
}
}
}
You could pass the file name to the 'call_function()' too, and sometimes I write the code with the output file stream specified. That function ('call_function()') is what processes one file - reading to the end of the file. It does not close the file; it was given an open file and should not close it.
The first 'if' deals with the I/O redirection case, of course.
I wrote, many years go, a function to handle this loop. It simplifies my life whenever I need to write a command in this 'UNIX filter' idiom - which is quite often. Along with a standardized error reporting package, it greatly simplifies life. Having it as a function also permits me to use variants on it, such as one that creates a backup of the file before it is overwritten, or which safely overwrites the file if the function completes successfully.
#R.. is correct for the usual cases.
If you want to have interactive behavior in case #3 but not #2, beyond letting the terminal buffer the user's input by line, you can use isatty (specifically isatty(0)) to determine whether there's a person on the other end.
This is not standard C, but neither is the notion of a terminal or a shell!
I'm wondering if there is a way to tell if a redirection is present in the shell command?
No. From your program's point of view there is no difference between these two cases:
./Myprogram < input.txt
./Myprogram
In both the cases the program is not taking any command line argument and would get it's input from the standard input.In the first case it's the shell that is connecting the contents of the file input.txt to the stdin of your program your program knows nothing about this.
It is possible to tell whether there is data to read by using select() on stdin. This will not tell you whether there is a redirection (you won't be able to tell when the file is empty, or when for some reason the user managed to put something on stdin before your program got a chance to test for it). Whether it works for your case or not depends on what you want to do in borderline cases.
Alternatively, you can use isatty() on stdin to find out if it's a tty or not. It's what most programs will do to find out whether they are interactive or not, and probably better in your case.
Now, you may notice that blocking waiting for user input in the third case is what all standard tools do, and is probably the behavior most users expect of your program too.