I have a compiled program that takes strings as input using stdin five times (in a loop).
Every time after taking input it does some work on it and gives an output.
I want to debug it gdb and I have to give it input from a file.
I cannot give input from keyboard directly.
I want to give the same string every time the program asks for input.
I have tried run < input.txt
But it seems to me that the program takes input from this file only the first time and after that it only reads null for other 4 inputs.
So I want to know if there is a way to solve this problem.
And am I right about the thing that input is read from file only the first time program asks for input and not read from the file rest of the times?
The run < input.txt and set args < input.txt and then run should work for all reads from stdin. Maybe you are onto the bug in your program that you are trying to find.
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 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.
What is difference between command line argument and scanf function in c?
1) what does command line argument mean?
2)If i can take input from user through scanf then what is the need of commad line argument.
3) what is principal difference between scanf and command line rgument
There are three types of standard inputs basically:
1.Compile time
2.Load time
3.Run time
1.Compile time: In this type the programmer himself gives input in code only while compiling.
2.Load time: Load time means when the program is being loaded into RAM for execution. In linux terminal in command prompt when you type ./a.out (Or any executable name) you are loading your executable file into RAM, which you've got after compiling it. So while loading the executable along with the executable whatever you are passing through command prompt is treated as command line arguments. And that can be used in code some where at run time. In short command line args is the input provided at load time.
3. Run time: Its the time while program is running or being executed, scanf () is one of the functions that can be used to provide input at run time. So with use of scanf () we can provide input to our program at run time basically.
They're two different ways of getting information into the program.
When you run a program with command line arguments, they're made available to the main function as parameters. Since they're C strings, you can read them as such. Running it with command line arguments is basically something like:
store picture_of_zx80.jpg myPornDirectory
That's running the store program with two arguments.
The scanf function, on the other hand, reads information in from standard input, something that needs to be provided separately to any command line arguments that may be provided.
A command line argument is added when you start the program.
e.g. notepad.exe myletter.txt
scanf reads information from the input pipe in other words after the program has started. it also can apply some formatting to the input data.
Command Line arguments are arguments you pass to your program when you start executing it, which then may be used in the program, for instance to control certain behaviour. They can be specified when you run it, for instance, if you had a program called test.exe, you could run it with
test.exe someArg
From the command line.
The scanf() function reads input according to what you specify from the standard input buffer stdin. In programs executed on the command line this is typically done to recieve user input, for instance like this:
int main(void) {
int input = 0;
scanf("%d",input);
printf("You inputted: %d",input);
return 0;
}
I'm currently working on a bash-like project. However, I need to test this project with several thousands tests, what cannot be checked manually. That's why I'd like to perform the tests automatically.
My program used fgets() to get user input. I know how to send arguments to program directly, but when the program is started, it displays the prompt of the program. How to cat a file containing tests on the stdin to make the program able to interpret tests? (and get the program output in the terminal).
Assuming all your test input is stored in a file called "test.input" (with each line being the complete input to the program) you can do the following in bash:
export LINE_NO=0
cat test.input | while read LINE; do echo "$LINE" | ./program > results.$LINE_NO;
LINE_NO=$(($LINE_NO + 1));
done
Basically, for each line in test.input, the above command will feed it to the program and store the results of the program in results.0, results.1, results.2, etc, where the number in results.number is the line number of the test.input file.
Hope this helps.