Working with standard input in terminal instead of using text file - c

I have a following problem: I am quite a beginner with C and a total beginner to linux terminal (I use fedora) and unix systems. I want to make a program with a table as input. The table should be given as standard input and the program should do several operations with the table such as printing chosen columns and rows, minimum of them maximum etc.
If I run with <table.txt everything is fine, but if I run program like this: ./program some parameters ENTER and then a type the table, program is doing everything but not what I want of it. And my question is: how can I work with terminal like with text file? I mean I want to type a row eith some columns, press enter, another row, enter......then type EOF (think it ctrl + D) ...and the program starts working after the table is finished (ctrl + D) is pressed.

Related

UNIX redirections from files for both input and output - that also shows what was the input in the output file

Sorry if I phrased the question a bit weird.
I'm trying to debug a program, and I'd like to redirect both the input and output from-to files, like this:
MyProgram.out << MyInput.txt >> MyOutput.txt
My problem with this is that I also need the output file to contain the input of every function.
For example, let's say that MyInput.txt has one line with the number 3, and MyProgram.out only asks from the user to input a number.
So what I'll get is:
Choose a number:
But what I'd like to get is (written in MyOutput.txt):
Choose a number: 3
Is there a way to make it show the inputs as well?
What you are trying to achieve is not possible, in the general case, without modifying MyProgram.out, as I answered here.
You are trying to interleave 2 pipes (input & output) in a synchronized fashion, where the synchronization is defined by MyProgram.out.
But pipes are asynchronous: the data (e.g. the line containing 3) has already been read from the file into the pipe before MyProgram.out even started, and now MyProgram.out is the one who decides whether to first read from the input pipe, and how many lines/bytes, or write something like Choose a number: to the output pipe.
So only MyProgram.out knows if Choose a number: should be written before 3, or vice versa.
There are some unclean ways you could still achieve that:
You could write a program that uses ptrace(2) to see if MyProgram.out reads the input first or writes the prompt first.
You could assume that every line of output corresponds to every line of input - which isn't the case in your program because when it writes Choose a number: it doesn't add a \n.
However, script only looks at the terminal, and you redirect the output from a file, not the terminal, so using script won't help you here.

Delete current terminal input line

I am trying to code a little console chat-program in C on linux.
So far I coded it in a way that both chatting partners are only able to alternately send/recv, because these function calls are blocking by default.
Now I would like to modify that program, so that both are able to send and receive simultaneously.
The problem that I find is, that once you typed some input to the terminal, I don't know how to output received messages, without messing up the current input line of the terminal.
If there was a way to delete that current input line, you could temporarily save that line, print the new message and put the input line right back.
However, I was not able to find a solution for this problem on the internet.
Is it possible to delete the current input line, and if not, how else could I achieve what I want?
I think you should look into ncurses as Edd said in his comment.
It would allow you to easily manage contents in your terminal window, which sounds like a good idea for your chat program.
All you'd need to do is store your messages in 2 character arrays:
char incoming[MSG_MAX]
and
char outgoing[MSG_MAX]
Then you can output those messages wherever you want in your terminal window, since ncurses allows you to specify x,y coordinates on where to put your text.
Then a simple wrapper for one of ncurses erase() family functions would allow you to delete characters from specify x,y coordinates in your terminal window.
Edit: MSG_MAX is not an actual ncurses macro.

test C program with some inputs

I wrote a program in C and I want to try it with some inputs. The inputs
are not numbers so I can't use a for loop or something like this in the code.
So I want to write a file with inputs and give it to the program so it will take every time (it waits to input) 1 line from the text file or something like this, is it possible?
If your program reads input from stdin - i.e. if you're using a function like gets or scanf to get your input - you can achieve what you're asking about by using input redirection. Let's say you create a file called test_input.dat which contains your test data. If this file and your program reside in the same directory, you can change to that directory using
cd \your_directory
and then run your program, telling it to read input from test_input.dat by doing the following:
your_program < test_input.dat
This will cause your program to read data from test_input.dat instead of from the terminal, and should work the same for most common operating systems (Windows or Unix variants such as Linux, etc).
Best of luck.

Getting keystroke from batch file

I am trying to achieve the following
I have a C program whose basic layout is as below and Press any key to continue is not starting of my program, this statement comes in middle of the program i.e; after taking some input commands then program comes to this point.
Let my program be stack_supporter.c . As an input it will take some options and perform some calculations and then comes to below point.
printf("Press any key to continue");
getchar();
calling_relevant_function();
I am using below batch file:
cd program_location_directory_path
./stack_supporter -o # -o is used to give options
and I am stuck in the middle of the program waiting for key press event. so how to automate these type of programs ?
echo(|myProgram.exe
You can pipe something into the program standard input

Why can't I read Ctrl+S in C?

I have this program in C that reads the input like this:
cod1 = getch ();
if (kbhit())
cod2 = getch ();
I can read every Ctrl+Char possible sequences, except for Ctrl+C, that closes the program - that is OK, and Ctrl+S, that simple is not catch. But I wanted to make Ctrl+S to be the save function in my program; how could I do that? Furthermore, is it possible to read Alt+Char characters? Because it reads it as a regular character, e.g., Alt+A is read with the same codes as A.
Your problem is that input probably gets eaten by terminal emulator.
For example Alt+<Whatever> is often reserved for menu shortcuts (e.g. Alt+F opens File menu). Matching characters are often hilighted once you hold Alt (F get's underscored in File).
Ctrl+S is reserved for Stops all output on screen (XOFF) (again your terminal emulator does that).
As for using Alt+<...> as shortcuts in your command line application. As far as I'm concerned holding Alt doesn't affect character received, it just sets flags which are hard to access in console. Even in GUI application (in Windows) it's quite tricky and you have to use function like GetAsyncState() to check whether alt was pressed.

Resources