- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
$ ./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?
Link Copied
5 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
if (file is input_001) then
...
elseif (file is input_002) then
...
elseif (file is input_003) then
.
.
.
end if
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Try INQUIRE (UNIT=5,NAME=charactervariable) and see what you get.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting Steve Lionel (Intel)
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.
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page