给出 5 个参数,但在终端中只得到 3 个 [英] Giving 5 Arguments but getting just 3 in terminal

查看:23
本文介绍了给出 5 个参数,但在终端中只得到 3 个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个文件传递给一个 c 程序.

I want to pass a file into a c program.

如果我在 IDE 中执行此参数

If I am doing it in the IDE this arguments

./test string string < test.txt

return argc = 5,但在终端上我只是得到 argc = 3.

return argc = 5, but on the terminal I am just getting argc = 3.

似乎是因为<"- 符号,我想用这个来表示我正在传递一个文件.

It seems, that its because of the "<" - symbol, I wanted to use this, to indicate that I am passing a file.

< 是什么意思?我正在使用带有 Tilix 终端的 Ubuntu

What does < mean? I am using Ubuntu with Tilix terminal

推荐答案

重定向由 shell 执行,对您的程序不(直接)可见.

Redirection is performed by the shell, and is not (directly) visible to your program.

./test string string < test.txt

意思是,

  1. 打开 test.txt 以读取文件描述符 1
  2. 使用参数 stringstring
  3. 运行 ./test
  1. Open test.txt for reading on file descriptor 1
  2. Run ./test with the arguments string and string

在第 2 点运行的程序将继承父级的文件描述符,因此其标准输入将连接到打开的文件句柄(而不是 shell 的当前标准输入,可能是您的终端或不同的文件句柄).

The program run in point 2 will inherit the parent's file descriptors, so its standard input will be connected to the opened file handle (rather than the shell's current standard input, which could be your terminal, or a different file handle).

顺便说一句,您可能希望避免调用您的程序 test,但只要您不忘记使用显式路径调用它,这是无害的.

As an aside, you probably want to avoid calling your programs test, though as long as you don't forget to invoke it with an explicit path, this is harmless.

这篇关于给出 5 个参数,但在终端中只得到 3 个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆