Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
If I want to use one exe file with the command like this:
1.exe < input.txt > output.txt
To enter the file name into the C program which is used to modify the input file and output file, will it work?
I have tried with 1.exe input.txt output.txt it works.
However, when I changed it into 1.exe < input.txt > output.txt, it could not open the file.
Even, I increased the pointer, argv[1] to argv[2] and argv[2] to argv[4].
So, what should I do with 1.exe < input.txt > output.txt command?
When you do this: 1.exe input.txt output.txt, it passes input.txt as the first argument to 1.exe and output.txt as the second argument.
This is different from 1.exe < input.txt > output.txt, which puts the contents of input.txt on stdin and writes stdout to output.txt.
The program 1.exe is expecting to be given the names of files which it then opens and operates on. It does not expect to read from stdin and write to stdout. If that's what you want to do, you need to modify the program to make it do that.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
I have a C program which reads its input from the command line, I would like to feed the executable with the output of ls | wc -m command, as I need to call two instances of the executable (./a.out1 , ./a.out2) using that same input and make them running in parallel (pipes ?).
Thank you for your help in advance!
You say you want to use a pipe, so first of all you need to adapt your program to read the input from stdin instead of argc and argv. Input passed via a pipe is not added to the command line argument list.
To pipe stdout of a process to multiple other processes, you can use tee and process substitutions:
ls | wc -m | tee >(./a.out1) >(./a.out2) >/dev/null
However, the reason why you require it be piped (as opposed to passed as an argument) isn't clear to me, so storing the output in a variable as suggested in the comments would work just as well for the example you present.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to debug a C program.
./test 1 2 3 << end
Monos(1,2)
Monos(6)
end
How to debug this?
For easier debugging with GDB, you should convert the "here string" lines between the <<end and end into a text file (say "input.txt"). Then, in gdb you can use the set args command to set up the command-line arguments and redirection of standard input from the file.
For example: suppose the file "input.txt" contains:
Monos(1,2)
Monos(6)
Run gdb from the shell as follows:
$ gdb ./test
Within GDB, set the command-line arguments and redirection of standard input:
(gdb) set args 1 2 3 < input.txt
Set any breakpoints, e.g.:
(gdb) b main
And start running the code:
(gdb) r
Don't do this redirection on the gdb command-line, but instead do it on the run command inside gdb.
https://sourceware.org/gdb/current/onlinedocs/gdb/Input_002fOutput.html#Input_002fOutput
This question already has answers here:
How to use redirection in C for file input
(4 answers)
Closed 1 year ago.
I'm very beginner-level in coding, C is the only language I have been learning. I've done thorough research on input redirection to a file to try to figure out how it works, but I do not understand where to use the command or exactly how it is used. My question is, where do I put the redirection command line in the program exactly? I know that it looks something like this: ./a < filename.txt , but I have no idea where to put it in the program, or if it even goes in the program? I want to read data from the files into a scanf using a simple loop. Also, the 'a', is that the exact name of the C program you are writing?
If you want to read from a redirection, then the program needs to read from
stdin:
int main(void)
{
char line[1024];
fgets(line, sizeof line, stdin);
puts(line);
return 0;
}
If you execute the program like this:
$ ./readline
then the user must enter the text and press ENTER.
If you execute the program like this:
$ echo "Hello World" | ./readline
Hello World
$ ./readline < filename
First line of filename
then stdin will be connected to the pipe / redirection. You don't have to
worry about this, the shell executing the command does the work (connecting stdin to the pipes, etc) so that
your program only need to read from stdin.
Same thing applies for stdout, if you want that the user calls your program
and uses the output in a pipe or redirection, then just write normally to
stdout. The shell takes care of connecting stdout to the pipe / redirection.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have Linux Ubuntu, and I want to test my program, for which someone gave me .txt file of multiple inputs. Now I want to run this program with inputs written in the .txt file. Theres a lot of inputs so I dont want to input them by hand. Is there some command in Linux Terminal to run a C with inputs written in a file?
thank you for your answers
I think you are suffering from the all too common misunderstanding that "standard input" == "a keyboard". Stop thinking that. If you've already written a program that reads from stdin, all you need to do is associate your text file with stdin. In the shell, you do that with a redirection operator:
./a.out < input.txt
If you have multiple inputs, you can easily invoke your program on each individually:
for file in *.txt; do
echo "Running on input: $file"
./a.out < "$file"
done
or you can run your program once on all the inputs:
cat *.txt | ./a.out
There are many, many ways to do what you want, and a lot of flexibility to do different things. You'll probably want to compare the output of your program with the expected output and then you're on your way to writing a full-fledged test suite. For example:
if ! ./a.out < input.txt | cmp expected-output -; then
echo "TEST FAILED" >&2
exit 1
fi
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In a programming competition , they mentioned this:
" How to answer to the problems?
In the problems you are up to solve, you have to read data from a file .IN, and write the results in a file OUT. "
I am used to the regular way of programming, writing the code in IDE , compiling , then executing it to see what's going on, and it worked, however my answers were refused.
Does anyone know anything about those file.IN and .OUT thing?
I mean, how can I get my program to take input from a textfile in which I write the dat aI want to give to the program, and make it send the the output to another textfile ?
Thanks
Include stdio.h in your header files.
In your main function, include the following lines on top.
freopen("input.in","r",stdin);
freopen("output.out","w",stdout);
In most online programming competitions, input is given through stdin and output through stdout. However, in this case, you have to read input from a file (.in) and write to a file (.out). freopen takes the stream(stdin, stdout) specified as the third argument to re-open the stream and instead use the file specified.
Edit: Sample code takes input from input.in and writes to output.out.
#include <stdio.h>
int main(){
freopen("input.in","r",stdin);
freopen("output.out","w",stdout);
int n;
scanf("%d",&n);
printf("%d\n",n);
return 0;
}