Fortran command to read command line argument? 
Author Message
 Fortran command to read command line argument?

Hi.

I am using f77 on an IBM RISC/6000 machine running under AIX4.1 (Unix).
I would need to read the arguments I pass to a certain program on the command
line. That is, I want to call

    prompt> interac fltplt.dat

and I want the interac program to have access to the string fltplt.dat.

Does anyone out there know if it possible to do such a thing in fortran? (In C
it seems quite straightforward).

Thanks a lot.

--
/---------------------------------------------------------------------------\
|        Philippe Jr Ngassam         |             \         /              |  
|------------------------------------|              \  (O)  /               |
| B.ing, etudiant M.Sc.A.            |     x_______---( o )---_______x      |
| Ecole Polytechnique de Montreal    |          x/  [_]`-'[_]  \x           |
| Genie Mecanique                    |                                      |
| (514)340-4981 FAX: (514)340-4052   |                                      |
-----------------------------------------------------------------------------

\---------------------------------------------------------------------------/



Sat, 24 Jul 1999 03:00:00 GMT  
 Fortran command to read command line argument?

Quote:

> Hi.

> I am using f77 on an IBM RISC/6000 machine running under AIX4.1 (Unix).
> I would need to read the arguments I pass to a certain program on the command
> line. That is, I want to call

>     prompt> interac fltplt.dat

> and I want the interac program to have access to the string fltplt.dat.

> Does anyone out there know if it possible to do such a thing in fortran? (In C
> it seems quite straightforward).

> Thanks a lot.

> --
> /---------------------------------------------------------------------------\
> |        Philippe Jr Ngassam         |             \         /              |
> |------------------------------------|              \  (O)  /               |
> | B.ing, etudiant M.Sc.A.            |     x_______---( o )---_______x      |
> | Ecole Polytechnique de Montreal    |          x/  [_]`-'[_]  \x           |
> | Genie Mecanique                    |                                      |
> | (514)340-4981 FAX: (514)340-4052   |                                      |
> -----------------------------------------------------------------------------

> \---------------------------------------------------------------------------/

Just read fltplt.dat into interac as a string variable!


Sat, 24 Jul 1999 03:00:00 GMT  
 Fortran command to read command line argument?

Hi Phillipe,

|> I am using f77 on an IBM RISC/6000 machine running under AIX4.1 (Unix).
|> I would need to read the arguments I pass to a certain program on the command
|> line. That is, I want to call
|>
|>     prompt> interac fltplt.dat
|>
|> and I want the interac program to have access to the string fltplt.dat.
|>
|> Does anyone out there know if it possible to do such a thing in fortran? (In C
|> it seems quite straightforward).

     There is no standard conforming way of doing this, but assuming that
you are using XL Fortran, you can do something like the following:

$ xlf90 blah5.f -o interac
$ cat blah5.f
      program p
        integer :: arg_no
        character(1024) :: cmd_arg
        do arg_no = 0, 4
          call getarg(arg_no, cmd_arg)
          print *, len_trim(cmd_arg), trim(cmd_arg)
        end do
      end program p
$ xlf90 blah5.f -o interac
** p   === End of Compilation 1 ===
1501-510  Compilation successful for file blah5.f.
$ interac fltplt.dat      
 7 interac
 10 fltplt.dat
 0
 0
 0
$

     getarg is a library routine that takes two arguments - the first is the
command line argument position (position 0 is the command name itself), and
the second argument is the value of the command line argument.

     I hope this helps.

Thanks,

Henry
-------------------------------------------------------------------------------
Henry Zongaro      XL Fortran/XL HPF Compiler Development
IBM SWS Toronto Lab   Tie Line 778-6044  Phone (416) 448-6044



Sun, 25 Jul 1999 03:00:00 GMT  
 Fortran command to read command line argument?

Quote:

> Hi.

> I am using f77 on an IBM RISC/6000 machine running under AIX4.1 (Unix).
> I would need to read the arguments I pass to a certain program on the command
> line. <snip>

Maybe the enclosed code can be useful.
--

M.Sc. Mech. Eng.            Baldersg. 4, SE-411 02 GOETEBORG, SWEDEN
Phone: Int + 46 340 66 75 87, Fax:   Int + 46 340 66 76 06

      logical function getclarg(iarg, ciarg)
c --- ------------------------------------------------------------------
c ... the purpose of function GETCLARG - GET Command Line ARGuments -
c     is to return command line argument number IARG in the character
c     variable CIARG, and to return TRUE if this was successful.
c
c     If argument number IARG does not exist, FALSE is returned.
c
c     In case of more serious troubles, execution is stopped in this
c     module.
c
c     Anders Henoch, Akustikon Partner , 1991-03-15
c --- ------------------------------------------------------------------
c 1992-02-03 AHH  Rewritten for UNIX/Silicon Graphics. Calls library
c                 functions GETARG and IARGC
c 1991-06-01 AHH  Assignment of CIARG removed in case of GETCLARG
c                 returning false.
c --- ------------------------------------------------------------------
c
      implicit none

c ... declarations parameter list
c
      integer iarg
      character*(*) ciarg

c ... local declarations
c
      integer*2         status2
      integer           liargc

      logical           test
      parameter (test = .false.)

      character*128     cline

      integer     lena
      external    lena

c ... unix declarations:
c
      integer iargc
      external iargc
c
C --- ------------------------------------------------------------------

      liargc = iargc()        ! ... get number of command line arguments

      if (liargc .lt. iarg) then
         status2 = -1
      else
         call getarg(iarg, cline)
         status2 = lena(cline)
      endif

      if (test) write(*,*) 'Status2 = ', status2

c ... (status2 .gt. 0) considered "normal" or "succesful"
c
      if (status2 .gt. 0) then
c
c ...... in addition we test lengths of character variables, thus
c        ensuring that nothing will be truncated. GETARG does not
c        check this.
c
         if (status2 .gt. len(cline)) goto 99903
         if (status2 .gt. len(ciarg)) goto 99902

         ciarg = cline
         getclarg = .true.
         return

      elseif (status2 .eq. -1) then
c
c ...... (status2 .eq. -1) indicates that the requested command line
c        argument was not present. This is "caught" by flag GETCLARG
c        above.
c
         getclarg = .false.
         return

      elseif (status2 .lt. -1) then

c ...... (status2 .lt. -1) indicates serious/unexpected problems
c        and will here stop execution.
c
         goto 99901

      else
         goto 99904
      endif

      return

c --- ------------------------------------------------------------------
c ... error returns

99901 continue
      write(*,*) '*** ERROR *** - GETARG call failed'
      write(*,*) 'STATUS = ', status2
      goto 99999

99902 continue
      write(*,'(1X, A)')
     &   '*** ERROR *** - insufficient length allocated for argument'
     & , 'number 2 in the module calling GETCLARG.'
     & , 'This argument must be a CHARACTER variable, large enough to'
     & , 'hold any command line argument to be read by GETCLARG.'
      write(*,*)
      write(*,*) 'At present the size is:        ', len(ciarg)
      write(*,*) 'but should have been at least: ', status2
      write(*,*) 'in order to hold the command line argument :'
      write(*,*) '-->', cline(1:lena(cline)), '<--'
      write(*,*)
      write(*,'(1X,A)')
     &   'Reduce the length of your command line arguments, '
     & , 'or recompile the module calling GETCLARG with an increased '
     & , 'length for the second argument to GETCLARG'
      write(*,*)
      goto 99999

99903 continue
      write(*,'(1X, A)')
     &   '*** ERROR *** - insufficient length allocated for local'
     & , 'variable CLINE in module GETCLARG'
     & , 'This argument must be a CHARACTER variable, large enough to'
     & , 'hold any command line argument to be read by GETCLARG.'
      write(*,*)
      write(*,*) 'At present the size is:        ', len(cline)
      write(*,*) 'but should have been at least: ', status2
      write(*,*) 'in order to hold the (here truncated) '
     &      // 'command line argument :'
      write(*,*) '-->', cline(1:lena(cline)), '<--'
      write(*,*)
      write(*,'(1X,A)')
     &   'Reduce the length of your command line arguments, '
     & , 'or recompile the module GETCLARG with an increased '
     & , 'length for variable CLINE'
      write(*,*)
      goto 99999

99904 continue
      write(*,*) 'ERROR - unexpected value of STATUS2 in module'
      write(*,*) 'GETCLARG'
      write(*,*) 'STATUS2 = ', status2
      goto 99999

99999 continue
      stop 'STOP in module GETCLARG'

      end

c lena
c
      integer function lena(string_in)
c --- -----------------------------------------------------------------
c ... the purpose of integer function LENA (LENgth Assigned) is to re-
c     turn the length of character*(*) variable string_in exclusive
c     trailing blanks or nulls
c     Anders Henoch, SIGMA ENGINEERING, 1987-11-09
c --- -----------------------------------------------------------------
c 1992-05-14 AHH  IBM AIX does not accept concatenation of string
c                 constants in parameter statements
c 1991-03-10 AHH  The searching for both BLANKS and NULLS was once in-
c                 cluded since uninitialized string variables in some
c                 environments did contain nulls.
c                 However, this may differ from installation to instal-
c                 lation, and any use of uninitialized variables must
c                 be considered bad programming practice.
c                 Therefore, a flag BLANKS_ONLY is introduced, which
c                 may be disabled if the comparison is to be done with
c                 NULLS, too.
c                 In addition, a flag COMPILER is introduced, which,
c                 if set to 'MSF', makes use of some MicroSoft Fortran
c                 extensions.
c --- -----------------------------------------------------------------
c
      implicit none

c ... declarations parameter list
c
      character*(*) string_in
c
c --- -----------------------------------------------------------------
c ... local declarations
c

c ... define if NULLS should be checked for
c
      logical BLANKS_ONLY, isblank
      parameter (BLANKS_ONLY = .true.)

      character*2 charset
c      parameter (charset = CHAR(32) // CHAR(0))

      integer*2 i, ic
c
c --- -----------------------------------------------------------------
c ... code begins here
c
      charset = CHAR(32) // CHAR(0)
      do i = len(string_in), 1, -1
         ic = ichar(string_in(i:i))
         if (BLANKS_ONLY) then
            isblank = (ic .eq. 32)
         else
            isblank = ((ic .eq. 32) .or. (ic .eq. 0))
         endif

         if ( isblank) then
c            do nothing
         else
            lena=i
            return
         endif
      enddo
c
c ... loop run to completion -> string_in consists of all blanks
c     and/or nulls -> lena = 0
c
      lena = 0

      return
      end



Sun, 25 Jul 1999 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Reading command line arguments with Microsoft FORTRAN

2. Read argument from the command line in Fortran

3. Reading Arguments from Command Line and reading binary data

4. command environments and command line arguments

5. reading command line arguments

6. Help: Reading command line arguments

7. Reading command line arguments in g77

8. Read argument from command line

9. Reading command line arguments

10. Parsing input arguments from command line and reading binary data

11. command-line arguments in fortran 90?

12. Fortran Command Line Arguments

 

 
Powered by phpBB® Forum Software