简体   繁体   English

从终端C程序输入文件

[英]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: 我正在开始一个C项目,我的教授希望我们使用以下格式来运行我们的程序:

./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 . 这是我的理解是,这意味着我在执行程序名为prog接受一个目录作为其参数(在这种情况下stop_words_dir ),并使用从命名的目录中的特定文件chapter.txt

  1. Am I correct? 我对么?
  2. If so, is this format Unix (more specifically bash) specific? 如果是这样,这种格式是Unix(更具体地说是bash)是否特定?
  3. What does the equivalent command in Windows look like? Windows中的等效命令是什么样的?
  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? 如何使用给定格式的C程序访问chapter.txt (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 . 外壳程序(任何类似于Unix的外壳程序-当然是Bash,而且还有Bourne,Korn和其他外壳程序,甚至包括C外壳程序系列)都从当前目录运行名为prog的程序,并使用字符串stop_words_dir作为参数,并使用其标准输入当前目录中名为chapter.txt的文件中的读数。

  1. If so, is this format Unix (more specifically Bash) specific? 如果是这样,这种格式是否在Unix(更具体地说是Bash)上特定?

The only thing that makes this Unix-specific is the use of / . 使该特定于Unix的唯一事情是使用/ On Windows, with the native shells, the / would lead to confusion (certainly for the cmd.exe 'shell'). 在Windows上,使用本机外壳程序时, /会引起混淆(对于cmd.exe “外壳程序”肯定是这样)。

  1. What does the equivalent command in Windows look like? Windows中的等效命令是什么样的?
    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. 首先,shell打开文件并处理错误(如果不存在)。 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. 其次,您可以仅使用scanf()getchar()从标准输入中进行读取,而无需使用fscanf()getc() (或亲戚)来读取数据。 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. 通常,您编写程序以使其处理任何给定的文件流,并在程序需要从标准输入读取时简单地传递stdin 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)? 我将如何在C程序(具有给定格式)中访问chapter.txt 。我知道如何使用两个参数来执行此操作?

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 …). 使用从标准输入读取的标准I / O函数scanf()例如scanf()getchar() ,或使用常规I / O函数但将stdin指定为文件流(例如fgets()fread()fscanf()getc()或…)。

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 据我了解,这意味着我正在执行一个名为prog ,该程序将目录作为其参数(在本例中为stop_words_dir ),并使用该目录中一个名为stop_words_dir的特定文件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. 您正在执行一个名为prog ,该程序接受任意数量的传入参数,在本例中为1( stop_words_dir ),并将标准输入重定向到当前目录( ./ )中的stop_words_dir的内容chapter.txt它可能在也可能不在stop_words_dir目录中。

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] . 请注意,必须在程序中使用argv的第二个索引argv[1]处理参数stop_words_dir

If so, is this format Unix (more specifically bash) specific? 如果是这样,这种格式是Unix(更具体地说是bash)是否特定?

I'm fairly certain that this is Unix specific, but I'm not positive. 我相当确定这是特定于Unix的,但我并不肯定。

What does the equivalent command in Windows look like? Windows中的等效命令是什么样的?

A Google search should provide you with the basic Windows cmd command to execute a program with one argument and redirected standard input. Google搜索应为您提供基本的Windows cmd命令,以执行带有一个参数和重定向的标准输入的程序。

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)? 我将如何在C程序(具有给定格式)中访问Chapter.txt。我知道如何使用两个参数来执行此操作?

The contents of chapter.txt are read using methods in C to retrieve keyboard input from a user, like scanf . 使用C方法读取chapter.txt的内容,以检索用户的键盘输入,例如scanf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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