Getting keystroke from batch file - c

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

Related

Vim: Compile and run C program in build in terminal

This question is quit similar to Vim [compile and] run shortcut
but what I want goes a little further. Is it possible to make a shortcut which compile and run the c code in the build in terminal and leave it open afterwards? The solution in the linked post just closes the output afterwards.
I guess the trick we used when coding Turbo Pascal and Turbo C++ would solve your problems. Just add a line for some dummy user input in the end of the program.
int main(void)
{
// Your code
getchar(); // Will not return to Vim before you have entered some data
}
You can combine !g++ % -o %< and :vert term ./%< together and make it a shortcut.
Here, ! allows to run external command from vim. g++ will compile the file, % indicates the current file, < is used to remove the file extension. :vert term is an internal command that lets you use terminal within vim.
Put the code in .vimrc file in home directory. The both commands combined would like,
map <F8> :w <CR> :!g++ % -o %< <CR>:vert term ./%<<CR>
When F8 button is pressed, vim saves the file then creates the object code. Afterward, with second command vim opens a terminal and runs the program. You will have to :q or type exit to close the terminal.
Make sure to exit insert mode before you hit F8.

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.

Automated Coverage Testing C Program using gcovr Linux with perl

I want to test 2 C programs to check it's coverage (line coverage and branch coverage).
I am using gcovr in linux system.
To do this, I have written a Perl Script that will call gcc compiler and read input from the file, then gives test for the coverage of C program.
I have many test suites in the form of files that are given to C program, I am using Perl to automate the input and get the coverage using gcovr.
Of course I've used system command , and backtics in perl to call gcc and gcovr.
The first C program runs well, because the input to the program is read from arguments given.
But for the second C program, I got confused, because the program only accepts input from keyboard or simply put "it is using scanf".
So, how can we provide input to C programs that only accepts input from keyboard (scanf) through terminal linux or using perl with system command ?
scanf accepts input from stdin. You can redirect stdin to a file when you start your program and have the program get its input from that file.
Redirection is a feature of the OS, not a feature of a specific language.
Let's say you want to start your program and you know that this program will accept '10' 'y' and 'n' as input. All you have to do is to create a file with those lines in, by whatever way you can. Let's suppose that this file is named test_1.
You then start the program with tested_program < test_1 and the program will use '10', 'y' and 'n' as its input.
If using the Perl command system is not a strict requirement, you can consider using Open3 instead. It is more advanced than system, and you can specify what to use for STDIN, STDOUT and STDERR. So you can write the input to your C program in a text file, create a handle to that file, and use Open3 to call your C program specifying that file handle as first argument.

Working with standard input in terminal instead of using text file

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.

Interactive input that doesn't appear on the in the window in C

I'm writing a program that needs to ask the user a yes or no question at the end. Going based off the example .exe my teacher provided us, the line is supposed to print out "Would you like to print an Amortization Table(Y/N)?Y" and it looks for one keystroke from the user. The Y is printed out following the question like I typed, as it is supposed to represent the default choice so if the user presses [y], [shift + y], or [enter] it goes to the function that does the amortization table and if the user presses anything else it goes to the next line When it gets the input from the user it processes the keystroke instantly as it is pressed (it does not need [enter] to process the input) without letting the keystroke appear on the command prompt. I have tried all the functions I can think of to do this (getc, getchar, getche) but everything I have tried ends up printing the user's input. Does anyone know what function he used or what trick he is doing to keep the keystroke from appearing in the command prompt? Thanks for your help in advance, I am obviously new to programming.
For the Windows platform, use the _getch() function from <conio.h> to read a keypress without buffering or echo.
Read the function description on MSDN

Resources