Expecting input from std::cin (Unix C++) [duplicate] - c++

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Detect if stdin is a terminal or pipe in C/C++/Qt?
I'm writing a command line application that expects data as either a command line argument, or from cin.
Is there a way to check if the user piped some data in the application ($ ./myapp < test.txt), and only display a prompt for keyboard input if not?
If I'm checking for !cin.good() / cin.eof() etc., the prompt will never appear.

isatty(STDIN_FILENO)
will return whether standard input is a terminal (tty), i.e. interactive.

Perhaps you can do something with fstat(2) and S_ISFIFO?

Related

C++ console insert text into displayed text [duplicate]

This question already has answers here:
Rewinding std::cout to go back to the beginning of a line
(3 answers)
Closed 4 years ago.
How can I rewind std::cout back to beginning of line and insert text without overwriting exiting one ? Can it be done using just standard c++ functions, or do I need low-level OS functions for console to do this ?
EDIT: I'm writing a simple telnet client. So when a message is received it should be appended at the top and user imput should not be overwritten.
No, you can't do this, and it's considered useless in console.
There is a function named std::seekp for all basic_ostream based class. But when you apply this to cout, no effect at all but failbit is set.
Use std::cout << '\xd' this line will output a carriage return which will fulfill your requirement. This this line will overlap your previous entry.

How can I print out the file I just inputted into my program in C++/C [duplicate]

This question already has answers here:
In C how do I print filename of file that is redirected as input in shell
(9 answers)
Closed 7 years ago.
I'm trying to make my program output everything I typed into the command line but the file I streamed to it is not printing out because it doesn't get stored in argv. Here is how I execute:
Input:
./program < file.txt
Expected output:
./program < file.txt
Actual output:
./program
Just to be clear I don't want to print out what is in the file. I only want to print out the name of the file.
File redirection is handled by the shell, not by the program. When the shell sees "<" it basically says "when you start this program, map stdin to this file instead of the terminal". So, under the hood, the shell does fork(); followed by closing and opening the file in it's place, then calls execv() or similar to actually execute the program. Similarly, if you do ./program *.txt, by the time the program sees the command line, *.txt has been expanded to all of the matching filenames in the directory.
I doubt, it's a stream of data, filename is not being streamed to the program
Besides, I don't think you should look for the input in argv, you should read stream contents from stdin, e.g. using scanf's

Run a command with parameter from c++ program [duplicate]

This question already has an answer here:
how to run a batch file using c++?
(1 answer)
Closed 8 years ago.
I want to run an .exe file from my c++ program.
but I also want to pipe an input file and take output of that file into another file.
I know that this can be done from command line as:
c:> my_program.exe <"input.txt"> "output.txt"
with this command, my_program takes all standard input from input.txt and gives standard output to output.txt
Now I want this should happen from my C++ program.
my my_program.exe is in D: drive. also input.txt is in D: drive.
Please tell me how can I accomplish my goal.
You need to handle input and output pipes inside your c++ program, and read/write data to files accordingly. See MSDN for example.
The question was basically how to redirect stdin and stdout from the inside of C++, which as been answered here.
Just change your directory to D:
cd D:\
D:>my_program.exe <"input.txt">"output.txt"

execute script with C++ [duplicate]

This question already has answers here:
How do I execute a command and get the output of the command within C++ using POSIX?
(12 answers)
How to assign shell command output to a variable in C language
(4 answers)
Closed 9 years ago.
I want to execute a script through a c++ program and get its output. Presently I am doing
system("./script.sh > out.txt");
But I need a command that get the output to a string, some thing like:
out = system("./script.sh");
printf(out);
I can't read the file out.txt after execute the script because I don't have permission to that. I deployed my c++ program at other framework (boinc) that doesn't give me this permission.
Does anybody have a hint?
Thanks in advance!
Felipe
you can use popen() and then get the output of the command from the pipe opened by popen()
FILE *fp;
fp=popen("./script.sh","r");
and to get your output. you can use fgets() or fread() to read from pipe like you read from a file

get output of system() into a variable [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Best way to capture stdout from a system() command so it can be passed to another function
In linux to get the current status of a service I wrote this code segment::
char cmd[100];
sprintf(cmd,"service %s status",argv[1]);
system(cmd);
It is running fine and it shows the output on the console like : mysql is running OR mysql is stopped
But I need this console output in a string variable. How can I get 'mysql is running' in a string variable so that I can use this string variable later.
thankx.
If you want to capture output then use popen() rather than system().