简体   繁体   中英

File input from terminal C program

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)
  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 …).

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM