1

I am beginning a C project, and my professor expects us to use the following format to run our program:

./prog stop_words_dir < chapter.txt

It is my understanding that this means I am executing a program named prog that takes a directory as its parameter (in this case stop_words_dir) and uses a specific file from that directory named chapter.txt.

  1. Am I correct?
  2. If so, is this format Unix (more specifically bash) specific?
  3. What does the equivalent command in Windows look like?
  4. What is the benefit to using the < as opposed to a second parameter
  5. How would I access chapter.txt in a C program with the given format? (I am aware of how to do this with two parameters)
indiv
  • 17,306
  • 6
  • 61
  • 82
Lance_P
  • 299
  • 2
  • 16
  • It appears unix, windows has backslashes rather than forward slashes - but windows accepts either so it will work on windows. Start a program named `prog` in the current directory (`.`), load a file, folder, or whatever it is of words with parameter 1 (the only parameter), then check standard input which has a file directed into it. See here for my list of punctuation http://stackoverflow.com/questions/31820569/trouble-with-renaming-folders-and-sub-folders-using-batch –  Apr 11 '16 at 02:00
  • If you are designing the program then there is not much advantage to piping, as in using "<" in "< chapter.txt". The "<" is designed for use with programs that someone else wrote. – Sam Hobbs Apr 11 '16 at 02:37

2 Answers2

6
  1. Am I correct?

Not entirely. The shell (any Unix-like shell — Bash certainly, but also Bourne, Korn and other shells, including even the C shell family) runs the program named prog from the current directory, with the string stop_words_dir as an argument, and with its standard input reading from the file in the current directory called chapter.txt.

  1. If so, is this format Unix (more specifically Bash) specific?

The only thing that makes this Unix-specific is the use of /. On Windows, with the native shells, the / would lead to confusion (certainly for the cmd.exe 'shell').

  1. What does the equivalent command in Windows look like?
    prog stop_words_dir < chapter.txt
  1. What is the benefit to using the '<' as opposed to a second parameter.

Primarily, the shell opens the file and deals with the error if it does not exist. If the < were not used, your program would have to do the opening, and handle the errors. Secondarily, you can use just scanf() and getchar() to read from standard input, rather than needing to use fscanf() or getc() (or relatives) to read the data. It is only a minor benefit. Typically, you write your program so that it processes any given file stream, and simply pass stdin when the program needs to read from standard input. The function is then more general — and reusable.

  1. How would I access chapter.txt in a C program (with the given format. I am aware of how to do this with two parameters)?

With standard I/O functions that read from standard input, such as scanf() or getchar(), or with the general I/O functions but specifying stdin as the file stream (such as fgets() or fread() — or fscanf() or getc(), or …).

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Upticked. the description of the IO redirection and its program-reuse value is spot-on (not surprisingly). And the distinction of OS vs. shell equally informative. Ok then, I'm off to delete the answer I was crafting =P – WhozCraig Apr 11 '16 at 02:27
  • I've instead given you the most helpful answer, as you answered each of my questions fully, although I do still appreciate Jonny Henly's answer. – Lance_P Apr 11 '16 at 02:32
  • I assume that the only reason why "./prog" is used is to force execution of the program from **only** the current directory. In Windows using just "prog" would tell Windows to search for "prog" in other specified locations. If I am correct about Unix/Linux then just using "prog" is **not** the equivalent in Windows. – Sam Hobbs Apr 11 '16 at 02:41
  • @user34660: I am not sure whether the `cmd.exe` always looks in the current directory when you type a simple command name. I think it does, but I am far from certain. If it does not, then the `./prog` on Unix becomes `.\prog` on Windows. – Jonathan Leffler Apr 11 '16 at 03:04
  • Instead of saying "command name", in this context I would say "console program", but that is strictly a difference in terminology and not important. What is important is that yes, Windows will definitely look in the current directory first. – Sam Hobbs Apr 11 '16 at 03:09
1

Am I correct?

No, but you are close.

It is my understanding that this means I am executing a program named prog that takes a directory as it's parameter (in this case stop_words_dir) and uses a specific file from that directory named chapter.txt

You're executing a program named prog that takes any number of passed in arguments, in this case 1 (stop_words_dir), and you're redirecting standard input to the contents of chapter.txt, which is in the current directory (./) it may or may not be in the stop_words_dir directory.

Note that the argument stop_words_dir has to be handled in the program by using the second index of argv, which is argv[1].

If so, is this format Unix (more specifically bash) specific?

I'm fairly certain that this is Unix specific, but I'm not positive.

What does the equivalent command in Windows look like?

A Google search should provide you with the basic Windows cmd command to execute a program with one argument and redirected standard input.

What is the benefit to using the '<' as opposed to a second parameter

< basically means switch keyboard input with the content of the input stream given, or redirect standard input. A second command argument would have to be dealt with differently, for example opening and reading from a file.

How would I access chapter.txt in a C program (with the given format. I am aware of how to do this with two parameters)?

The contents of chapter.txt are read using methods in C to retrieve keyboard input from a user, like scanf.

Jonny Henly
  • 4,023
  • 4
  • 26
  • 43
  • Ah, thank you very much. This answered most of my questions, everything else I need to know I will be able to find out now. Unfortunately trying to google "use of < in terminal" is not very helpful. Answers will be much easier to find now that I know that it is called the redirection of standard input. – Lance_P Apr 11 '16 at 02:11
  • @Lance_P no probelm at all, let me know if you need any more help. You're right that trying to google the meaning of non word operations like `<` doesn't always yield helpful results. – Jonny Henly Apr 11 '16 at 02:21
  • It might help to explain how it can be determined that prog "takes any number of passed in arguments". We know it takes one but how do we know it can take more? – Sam Hobbs Apr 11 '16 at 02:44