Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28465 Discussions

how to catch the standard input file name?

yyxt11a
Beginner
386 Views
I want to read in data from a batch file by using the following command:

$ ./prog < input_001

Where prog uses the data from input_001.

If I have many input files as input_002, input_003 etc., how can I let the program know the name of the input file, so that it can do things accordingly?
0 Kudos
5 Replies
mecej4
Honored Contributor III
386 Views
If what you want to do is to take as input the combined content of input_001, input_002, .., input_004 in that order, use:
[bash]cat input_001 input_002 input_003 input_004 | ./prog
[/bash]
If you need to use as input many more files, and you are sure that they are all present and contain the correct data, you could use:

[bash]cat input_* | ./prog
[/bash]
0 Kudos
yyxt11a
Beginner
386 Views
This is what I wann do in `prog`:

if (file is input_001) then
...
elseif (file is input_002) then
...
elseif (file is input_003) then
.
.
.
end if
0 Kudos
Hirchert__Kurt_W
New Contributor II
386 Views
To the best of my knowledge, there is no way to get a file name for STDIN, because in the general case STDIN may not have a file name (e.g., it might be a pipe).

On the other hand, if you are willing to change your program invocation from

$ ./prog < input_001

to

$ ./prog input_001

you could use the intrinsic subroutine GET_COMMAND_ARGUMENT to retrieve the first command argument as a character value (in this case, 'input_001'), and then use that character string both in your tests and in an OPEN statement, so you can read the file named.


Other approaches might involve wrapping your program in a shell script. For example

#!/bin/bash
echo $1 | cat - $1 | ./prog

would make your program see the filename on a line before the actual file contents, or

#!/bin/bash
./prog $1 < $1

would leave the file on STDIN and also make the name a command argument acessible through GET_COMMAND_ARGUMENT.

-Kurt
0 Kudos
Steven_L_Intel1
Employee
386 Views
Try INQUIRE (UNIT=5,NAME=charactervariable) and see what you get.
0 Kudos
yyxt11a
Beginner
386 Views
Try INQUIRE (UNIT=5,NAME=charactervariable) and see what you get.

Many thanks for all the above answers. I've tried steve's method, and INQUIRE spitted out the name of the input file as '/dev/pts/67', which is not the original 'input_001'. However, correct file names were obtained when I tried to iINQUIRE non STDIN files.

0 Kudos
Reply