How can I open the file when I type < (file name) in terminal?

Asked 2 years ago, Updated 2 years ago, 155 views

For example, when running a program called p1,

./p1 < file_name.txt

I want to write a program to get the file name and process it after typing it like this Skip < file_name.txt and enter argv from the next entry

< file_name.txt I want to read the file in this way without changing it How should I deal with it?

terminal c

2022-09-22 18:27

1 Answers

Stackoverflow Same question

I'm wondering if there is a way to tell if a redirection is present in the shell command?

I wonder if there is a way to tell the shell command about redirection

No. From your program's point of view there is no difference between these two cases:

./Myprogram < input.txt

./Myprogram

In both the cases the program is not taking any command line argument and would get it's input from the standard input. In the first case it's the shell that is connecting the contents of the file input.txt to the stdin of your program your program knows nothing about this.

 is missing. from a program's point of view 
There is no difference between the two cases below.
Two examples:
./Myprogram < input.txt
./Myprogram
In both cases, the program does not acquire command line arguments
Redirection receives input from standard input.
In the first case, the contents of the input.txt file are: 
The shell that connects to the program's stdin. 
The program knows nothing about this fact.


2022-09-22 18:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.