-3

I'm trying to make my program output everything I typed into the command line but the file I streamed to it is not printing out because it doesn't get stored in argv. Here is how I execute:

Input: ./program < file.txt

Expected output: ./program < file.txt

Actual output: ./program

Just to be clear I don't want to print out what is in the file. I only want to print out the name of the file.

  • 2
    Your question results from a misunderstanding of what the `<` operator does. It does *not* pass any arguments to the program. All it does is redirect stdout/stdin. So, in this case, `./program` will be executed, and then the contents of `file.txt` will be loaded onto stdin as if it were typed on the keyboard. `program` has no idea `file.txt` even exists. – aruisdante Mar 08 '15 at 03:04
  • I understand now but so is there no way to get the file name printed out through my program? – user3487386 Mar 08 '15 at 03:06
  • just remove the `<`. `"file.txt"` will be the second string in the argument list (the first is the program name). Of course you'll lose redirection, so the contents of stdin will no longer be primed with the contents of `file.txt`. – aruisdante Mar 08 '15 at 03:08
  • hmm well that loses the whole point of having the file there. ha. But thank you very much for explaining this! – user3487386 Mar 08 '15 at 03:13

2 Answers2

2

File redirection is handled by the shell, not by the program. When the shell sees "<" it basically says "when you start this program, map stdin to this file instead of the terminal". So, under the hood, the shell does fork(); followed by closing and opening the file in it's place, then calls execv() or similar to actually execute the program. Similarly, if you do ./program *.txt, by the time the program sees the command line, *.txt has been expanded to all of the matching filenames in the directory.

Charlie
  • 1,487
  • 10
  • 9
  • Thank you. This along with "aruisdante"'s response have helped me understand. But is there no other way like through the shell. Some command that prints back the input? Thanks again. – user3487386 Mar 08 '15 at 03:09
  • In this case "< filename" isn't input to the program, it's input to the shell. If you wanted to do "./program a b c d e" and print that out, you could easily loop over argv and print the arugments. If you're on linux, you could start playing around in /proc/self and maybe make some educated guesses if stdin/stdout/stderr were redirected, but getting the whole thing right is non-trivial (and you can't always get it in the same order as the caller). – Charlie Mar 08 '15 at 03:17
  • FWIW - it's worth learning how a shell processes command lines, or even implementing your own simple shell that can handle I/O redirect. – Charlie Mar 08 '15 at 03:21
  • A good and clear explanation. +1. – shauryachats Mar 08 '15 at 03:27
0

I doubt, it's a stream of data, filename is not being streamed to the program Besides, I don't think you should look for the input in argv, you should read stream contents from stdin, e.g. using scanf's