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.
Related
I am trying to make a program that can process sentences in C in the POSIX environment. Assume that my program's name is "test". If the user entered just "./test", then my program will ask the user to enter some sentences. This one so far is easy.
However, if the user entered "./test < file.txt", the program should get the characters from that txt file. I do not know how I can get the characters of the file in C. I tried something like file = open(argv[2]);, but it did not work.
I will really appreciate it if you give me the answer to this question.
TL;DR: If you start your program like
./test
and you have to type in the input, then exactly the same program will read from file.txt if you start it as
./test < file.txt
Longer explanation starts here. (The following explanation is not 100% precise, but shall help to get an understanding what is going on in principle.)
In a C program you can open files with fopen. As a return value, fopen gives you a FILE pointer. However, when you start a program under Unix, three FILE pointers are already available. These default FILE pointers are stored in variables named stdin, stdout and stderr.
Of these, stdin can be used to read from, stdout and stderr can be written to. And, stdin is used as default in several C library calls, like, gets or scanf. Similarly, stdout is used by default for calls like printf.
Now, although they are called FILE pointers, they can in fact represent other things than just files. stdin could be a file, but it can also be a console where you can type in stuff.
This latter scenario is what you observe when you start your test program from the shell with the command
./test
In this case, the test process will be started with stdin just using the console from the shell from which you started the test program. Therefore, if in your test program you call, say, gets(), then your program will implicitly read from stdin, which represents the console input that was inherited from the shell. Consequently, in this case the user has to provide input by typing it in.
Now let's look at what happens if you start your process from the shell in the following way:
./test < file.txt
Here, the shell does a bit of extra work before it actually creates your test process. This is because the < file.txt part of your command line is interpreted by the shell - this is not passed as arguments to your program. Instead, what the shell does is, to open the file.txt and, when the test process is started, hand the opened file.txt over to the process such that in your test process stdin is connected to file.txt.
Then, the call to gets() in your program will again read from stdin, but this time stdin is not the console. This time stdin really corresponds to a file, that is, file.txt.
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.
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.
In section 1.6 of 'The C Programming Language' (K&R) there is an example program and the authors state:
The output of this program ON ITSELF is
digits = 9 3 0 0 0 0 0 0 0 1, white space = 123, other = 345
Then there are many similar programs etc. including exercises. I understand everything about the logic of the code and can do the exercises, but how do I actually test my program on a file like the authors or many others on the web?
Edit: The question should be: How can I redirect the input of a file to the program?
The program in chapter 1.6 reads input from stdin. To make it read from a file, you can (on most operating systems) redirect stdin to be a file by running your program like this:
myprogram < somefile
Or you can pipe the content of a file to it like so:
cat somefile | myprogram
On windows, you'd use the type program instead of cat,
type somefile | myprogram
This is using re-direction. Instead of the input to the program coming from the keyboard it comes from a file.
At the DOS prompt:-
C:>myexe < filename
Get to the DOS prompt in Windows use the command shell. Or start Run.. and enter cmd
On a Mac this is called terminal (type "terminal" into Searchlight to get to it).
By default, your program will take input from stdin, which is a buffer which is filled based on input from your keyboard (by default). However, you can also tell your program to fill stdin from a text file instead.
Using a *nix based system, you can simply create a text file, and save it as whatever you'd like, "test_input" for instance. Fill it with the input that you'd like to pass to your program, save it, and then run your program like this:
./a.out < test_input
This is called redirection because you are "redirecting" (if you will) the input to come from a file, rather than the default (keyboard). It goes both ways, you can also redirect your output to a file, rather than stdout with the other angle bracket, '>'.
Using Visual Studio, and not popping open a command prompt to do something like the command above, you can use a C++ ifstream, put the text file in the local directory, and then simply use the ifstream everywhere instead of stdin:
ifstream sin("test_input.txt" , ifstream::in);
int value;
sin >> value;
You can output to a file using an ofstream.
Note that ifstreams and ofstreams are C++ objects, and can't be used in C. While you can write to files and read from files in C, it's a little trickier than simply replacing all instances of cout and cin. You actually have to think about what you are reading and writing :)
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.