How to read output and give input to a program from c program? - c

This is with reference to the below question:
Execute program from within a C program
How do I do the same on Windows with Tiny C Compiler?
I need to execute a .exe fro c program and give input to it from within the same C program by using a file or string as source and read the output from it into a string or file.I have been using system() frunction. Any suggesstions or examples are welcome..Thanks in advance

The simplest way if you don't have popen() etc, or you want to avoid the complexity, is to simplly write a data file eg. infile with fwrite() execute the external program with system() and then read outfile.
system("prog.exe <infile >outfile")
Your prog.exe only has to read stdin and write stdout.
This way you can easily test it with the contents of in/out file.
You would normally do this in your tmp directory and then delete them when you are finished.
The only thing to be careful of is the path to the .exe

Google for "windows popen" and you find this link:
lists.trolltech.com/qt-interest/1999-09/thread00282-0.html
The C runtime library also has _popen(), but I would recommend against it.

Related

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.

Read file to standard in for parser

I am trying to implement a shell program in a linux environment. The part I am having trouble with is reading a setup_file inside of a shell before running the shell, to do things like set environment variables.
Currently the shell has a parser_results = parse() function which does a "getchar" and waits until the user types something into stdin, then does an execute(parser_result) which executes the command using the output of the parser.
What I want to do is to read the setup_file which has commands inside of it, have the parser read them in and give me the data structures I need. Then I can run execute.
My question is is how do I redirect the contents of the file to stdin? And how do I call the parser to parse this redirected input? I have been playing with dup and dup2 to no avail.
Short answer (to the question 'how do I redirect the contents of the file to stdin') is "You Don't".
You revise your input function to read from a given file stream instead of stdin, and then for reading from the file, you open it and pass that file pointer to your parsing code (and close when the parsing code is done), and then when you're ready for user input, you call the parsing code with stdin instead of the file. That saves fiddling with stdin.

(Asterisk PBX) How to control program written in C from asterisk agi in features.conf

How to control programs written in C from asterisk AGI-application in features.conf?
I want to control my program by pushing keys on the telephone (dtmf-tones). I was reading about using pipes for passing on standard I/O. My first idea was:
$ Asterisk | c_program
then have standard output in AGI-script by printf()
The second idea was to use:
$ printf parameter_a >> file
to write to file and then let my c-program read the file and evaluate the contents as parameter.
Has anyone tried out or has experience with similar tasks/problems?
there are no way do like u show.
posible solutions:
1) Run your program with arguments using system(). you can do that with or without AGI.
2) Use linux pipe(special files)
3) Start your program as AGI/EAGI script, your program must work acordinly, see CAGI
4) Use database table(task) and asterisk realtime for put record in it.
most correct is 3) if ur program is not demon and 2,4 if ur program is demon.
also you can use in dialplan

How to redirect the output of a c program to a file?

I am trying to redirect the output of a c program to file, even when it generates some errors because of problems with the input data. I can send the output but the error messages to a file.
Does somebody know how to do it?
From within C source code, you can redirect outputs using freopen():
General outputs:
freopen("myfile.txt", "w", stdout);
Errors:
freopen("myfile_err.txt", "w", stderr);
(This answer applies to bash shell, and similar flavors. You didn't specify your environment and this sort of question needs that detail.)
I assume you know about basic redirection with ">". To also capture STDERR in addition to STDOUT, use the following syntax:
command > file-name 2>&1
For some more background on standard streams and numbers:
http://en.wikipedia.org/wiki/Standard_streams#Standard_input_.28stdin.29
This depends on what you mean and what platform you are using. Very often you can accomplish this from the command line, which has been covered in another answer. If you use this method to accomplish this you should be aware that FILE * stderr is typically written immediately (unbuffered) while FILE * stdout may be buffered (usually line buffered) so you could end up with some of your error messages appearing to have been printed earlier than some other messages, but actually the other messages are just being printed late.
From within a C program you can also do something similar within the stdio system using freopen, which will effect the FILE *, so you could make fprintf(stderr, "fungus"); print to something besides what stderr normally would print to.
But if you want to know how to make a program redirect the actual file descriptors under a unix like system you need to learn about the dup and dup2 system calls. They allow you to duplicate a file descriptor.
int fd = open("some_file", O_WRONLY);
dup2(2,fd);
close(fd);
This code will make "some_file" the new stderr at the OS level. The dup2 call will close and replace file descriptor 2 (stderr, which is usually used by FILE * stderr but not necessarily if you call freopen(x,y,stderr) since that may make FILE *stderr use a different file descriptor).
This is how shell programs redirect input and output of programs. The open all of the files that the new program will need, fork, then the child uses dup2 to set up the files descriptors for the new program, then it closes any files that the new program won't need (usually just leaving 0, 1, and 2 open), and then uses one of the exec functions to become the program that the shell was told to run. (some of this isn't entirely accurate because some shells may rely on close on exe flags)
Using a simple linux command you can save the output into the file. here is a simple linux terminal command.
ls > file.txt
The output of this command will be stored into the file.
same as you can store the output of the program like this suppose, object file name is a, run the following command to save output in a file:
./a > file.txt

Resources