I want to redirect file to stdin
So I use .txt to test and practice. The tutorial says that redirection is like copying the content of the file to the stdin, and wrtie what should appear in the command window to another file
So, here is my code
int main(int argc, char ** argv) {
printf("%d arguments\n", argc);
for(argv; *(argv); printf("%s\n", *(argv)), argv++);
return 0;
}
in the command shell I typed $a < in.txt > out.txt
inside in.txt, the content is
Hello World
Test File
Here we go
I'm expecting the out.txt to have
4 arguments
a
Hello World
Test File
Here we go
but it turns out that it is
1 arguments
a
why the redirection from txt to stdin doesn't work?
It seems like < in.txt didn't copy the content to the console at all.
Redirecting input and output with pipes does not populate argv; it simply points stdin and stdout elsewhere, from within the execution environment. This is transparent to your program.
argv contains command-line arguments that are passed to the program itself, not the execution environment.
Related
I am learning the redirect symbol in Unix.I want to use redirect symbol '<' to transmit parameter for main to automatically change argv[1] in main.
Code in main is shown below
`
int main(int argc, char* argv[])
{
switch (atoi(argv[1]))
{
case 1:
.....
break;
default:
break;
}
return 0;
}
`
If i type ./a.exe 1,the main function will work in case 1 and argv[1] is 1.However, if i create a script.txt,the content of which is 1 and type ./a.exe < script.txt,case 1 will not work.And i check the argv[1] is null,which is supposed to be 1.
My code is running under Windows10 msys2 and compiler is gcc. Could anyone tell me how can i use '<' or other method.
If my understanding of '<' is wrong or there is something special for use this way to send argv[1] of main
The redirect operator is different from arguments. Actually, the redirect operator moves the content of the given file into stdin or move the output into a file. Let me explain it more. Consider the following example:
./a.out > out.txt
This redirects your stdout (printf output written into stdout for example) and writes it into out.txt.
./a.out < in.txt
This redirects in.txt into your stdin (scanf reads from stdin for example).
As you can see, these are different thing and not related to your arguments. If you want, following script reads the arg.txt and put its content as arguments for your code.
./a.out $(cat hello.txt)
So I understand that to read and print out a line of text you can just use printf, scanf and type it out. However what if I want to print out a text file without typing them out in terminal? And I don't mean using fopen(filename, "r") where you can only open a specific file. I think this is called redirection but I'm having trouble understanding it. Something along the line as the below input example:
./myprogram < input.txt
Here is a redirection cheat sheet. The line that interest us is:
cmd < file: Redirect the contents of the file to the standard input (stdin) of cmd.
Here a simple example that will print the content of your input.txt file. Compared to manual input, the program will never wait and will loop until the end of the file is reached (Note: there are cases where there is no end, you might want to add alternative break condition).
#include <stdio.h>
int main(void)
{
char buffer[100];
while (fgets(buffer, 100, stdin))
printf("%s", buffer);
return (0);
}
./myprogram < input.txt will print your input.txt
./myprogram will wait for your manual input and print what you just typed.
This is not exactly what you asked but you can put the filename as argument and get it in argv[1] and then use fopen
I'm trying to read command line arguments that have been redirected from a file. The command I'm using is ./a.out < test.txt
And the contents of test.txt is: Hello world.
But the output of my program below isn't printing Hello
world. Instead it is only showing ./a.out. Why is this?
int main(int argc, char* argv[], char* envp[]) {
for (int i = 1; i < argc; i++) {
printf("%s\n", argv[i]);
}
}
The shell intercepts the redirection commands before preparing the command line for the program:
myProg <infile -t >outfile
will pass to the program
myProg -t
with stdin and stdout already rerouted before the pogram starts. So the program never sees the rediretion.
There a lot of cases, besides simple derirection:
dir > myfile.txt
Especially you can pipe output from one program to another:
dir | more
It will send output if dir command to more command. Since program launch handled by OS shell, it handles a redirection too.
Because the language is defined that way. Suppose what you say is true —
All the user input will have to come from command line arguments, but text redirected from a file can satisfy input required in different functions. This can be achieved if the input appears as command line arguments.
Consider this program:
#include <stdio.h>
int is_dict(char *word)
{
/* code to look up a dictionary */
int result = 1;
return result;
}
int main(int argc, char *argv[])
{
if(argc == 2 && is_dict(argv[1]))
printf("%s found", argv[1]);
return 0;
}
If the program is written that way to accommodate it, then the input would have to come from the command line arguments. How would you take input when it is not redirected? It would require more program overhead to detect the missing inputs.
Moreover, imagine a text file containing a million words: it is unfeasible to expect each word to arrive as an argv[n].
There are other objections too. Suppose the program prints a series of prompts for responses. The user would have to know in advance what the prompts are, to supply the answers before the prompts appear.
Lastly, if the program is run from a GUI, then all the program's input will have to be edited into its properties before it is run.
im trying to test how my program is receiving a users command line input:
my command line input to test is:
"./concordance 15 < input.txt"
the rest of the program works but to test the arguments. so in my main function i have this:
int main(int argc, char *argv[])
{
int i;
for (i = 0; i < argc; i++)
{
printf("%s\n", argv[i]); //runs through command line for arg
}
printf("%d\n", argc); //prints total arguments
return 0;
}
The problem is when I enter my command line, the program prints:
./concordance
15
2
for my program to work I need to open the input.txt file so my question is, why is the program only printing "./concordance", and "15" aswell as only seeing 2 arguments if I have "<" and "input.txt" in the command line?
Thanks
< is not interpreted as a command line argument but is instead interpreted by the shell to redirect standard input to be the specified file instead of the parent standard input, typically the shell.
There is a difference between the command line and "stdin" - "15" is a command line argument. The shell sees the '<' and "redirects" the file to your program on it's stdin stream.
If you don't want to use stdin to process the file, just pass it's name and open yourself: ./concordance 15 input.txt (argv[2] will be input.txt)
I am trying to pass File1.txt ">" File2.txt as terminal arguments to my program in order to override the cat command. But for some reason, the program is not working. Although the argc is 4 in above defined case but still the condition in the program is not getting true. Here is the code:
int main(int argc, char *argv[])
{
int readbytes,fp;
char buf[1024];
if(argc==2)
{
fp=open(argv[1],O_RDONLY);
dup2(0,fp);
close(fp);
readbytes=read(STDIN_FILENO,buf,1024);
write(STDOUT_FILENO,buf,readbytes);
}
if(argc==4)
{
printf("inside4");
fp=open(argv[1],O_RDONLY);
dup2(fp,0);
close(fp);
fp=open(argv[3],O_WRONLY|O_CREAT|O_TRUNC,S_IRWXU);
dup2(fp,1);
close(fp);
readbytes=read(STDIN_FILENO,buf,1024);
//printf("%c",buf);
write(STDOUT_FILENO,buf,readbytes);
}
return 0;
}
I couldn't find a solution to this issue so I leave it to experts now.What is the reason for this problem?
NOTE:
For some reason when I send ./prog File1.txt > File2.txt to program, argc==2 condition is selected, however argc is 4. Why is that?
Regards
This is likely being caused by how you are running your program. Typing
./myProg foo > bar
will instruct most shells to run myProg with argument foo and save whatever is printed to stdout in a file named bar. To pass foo, >, and bar as command line arguments, use
./myProg foo \> bar
or
./myProg 'foo' '>' 'bar'
Side note: Because piping output into a file using > is part of the shell, not a program like cat itself, you likely shouldn't have to worry about it. Just write to stdout and the shell will handle the rest.
What do you mean by the condition in the program is not getting true? Are you saying that you don't see "inside4" printed to the terminal? There are a few things to consider. First, you do no error checking. We will have to assume that all of your open and dup2 calls succeed. I would expect that "inside4" is getting printed to the end of the output file. The reason for that is simply that printf does not actually write anything. It just stores the string "inside4" in a buffer, but that buffer is not written to the output until your program exits, and by that time the underlying file descriptor has been changed to the output file. The simplest fix is to append a newline to the output, and write printf( "inside4\n" ); In the normal setup, printing a newline causes the internal buffer to be flushed. You can also explicitly flush the buffer after calling printf by calling fflush.