-2

Just a simple question about command line stuff in C.

What does the below mean? I get that -a is an option for the execuble test. test-a < test.txt

But what does '< test.txt' mean?

Also if you guys could help me understand how a simple program would take in options like '-a, -b, -c' as being three different options, that would be helpful. Off the top of my head it seems like the way to do this is just checking if that argument has 2 characters and it just is a '-' followed by a letter for a valid option, but is there any special way to do this? Like is there something in C that automatically recognizes a '-' followed by a letter as something like an option ?

user2353398
  • 79
  • 1
  • 10
  • This post might help you http://stackoverflow.com/questions/498320/pass-arguments-into-c-program-from-command-line – user376507 Feb 16 '14 at 22:22

3 Answers3

1

In C it doesn't mean anything special, just a "<" on the command line.

In many OSes though it means to redirect standard input from the file test.txt. I.e. instead of stdin coming from the terminal, it comes from the named file.

To handle command line parsing for you, look at getopt.

< edit > I added this as a comment, but I think it deserves to be in the answer:

If you enter app -a -b < xxx, the shell invoking your application interprets the < xxx part of the line and < xxx won't be in the array of args (argv) passed to your application. You have no (easy) way to know that the redirection has occured.

John3136
  • 28,809
  • 4
  • 51
  • 69
  • So does mean it pulls out all arguments from this file? If I had something like "The quick brown fox" in that text file, how would it be read? – user2353398 Feb 16 '14 at 22:17
  • If your program is doing `scanf` or stuff like that it comes from the file instead of you typing intp the terminal. File IO is still file IO - If you are opening a file to read, then redirection is not what you want. – John3136 Feb 16 '14 at 22:18
  • Wait, I'm confused. What is the difference between having test < testa.txt and test testa.txt If all I'm being passed in for argv[0] is testa.txt in each. – user2353398 Feb 16 '14 at 22:24
  • If you enter "app -a -b < xxx", the shell invoking your application interprets the "< xxx" and it won't be part of argv[]. – John3136 Feb 16 '14 at 22:38
  • So then how do I actually read the stuff in 'xxx' within my application? Can you give any examples of the utility of this? Do I just treat it as another input stream? – user2353398 Feb 16 '14 at 22:43
  • Any time you do a `scanf` or other input method that isn't using a file you'll be getting data from xxx. – John3136 Feb 16 '14 at 22:48
1

I'd like to clarify what has been said so far by John3136 and enedil. I hope you accept one of those answers.

The shell (e.g., bash) is the program in charge of processing the commands you type and executing the appropriate program. The snippet < test.txt is a special directive that instructs the shell to make the program read its input from a file (in this case, text.txt). Words which aren't directives are passed to the C program as arguments through variable argv as enedil pointed out.

edit. There are two ways to pass a file to a program.

Providing the path to the file as an argument at a runtime. You can then call fopen to open the specified file:

if (argc == 1) {
    fprintf("No filename provided!");
    exit(EXIT_FAILURE);
}
FILE* f = fopen(argv[1], "r");
/* Do stuff with f here */

The other option is to just read your input from stdin in your C program, and redirect a file using < test.txt whenever you want to use the contents of a file as stdin. This way you give more flexibility by allowing the program to either read the input from a file or receive user input.

MBlanc
  • 1,773
  • 15
  • 31
  • So then can you help me understand if I want to read the contents of the test.txt, how would I go about doing it in C? – user2353398 Feb 16 '14 at 22:33
  • It depends a lot on what you are trying to do, I'll edit it into answer. – MBlanc Feb 16 '14 at 22:37
  • Okay, well could I fopen test.txt if it was redirected like that? I understand how to use fopen and fscanf, but I'm troubled by the difference between commandline args and redirected input in terms of me writing a program taking in a filename to fopen within it. – user2353398 Feb 16 '14 at 22:50
-1

The "< test.txt" means that the content of test.txt is passed as an argument. If "<" was replaced by ">" it would redirect the output of the program to test.txt.

I don't think there is a special way to detect "-". Iterate through arguments and check if the first character is "-"

Freaxy
  • 93
  • 5