2

我正在尝试从命令行将参数传递给 Fortran 可执行文件。在 C 中实现此目的的示例程序是(取自此处):

#include <stdio.h>

int main (int argc, char *argv[])
{
  int count;

  printf ("This program was called with \"%s\".\n",argv[0]);

  if (argc > 1)
    {
      for (count = 1; count < argc; count++)
    {
      printf("argv[%d] = %s\n", count, argv[count]);
    }
    }
  else
    {
      printf("The command had no other arguments.\n");
    }

  return 0;
}

这个程序的输出是:

This program was called with "./fubar".
argv[1] = a
argv[2] = b
argv[3] = c

我现在的问题是,我将如何在 Fortran 中编写这个程序(以及这个功能)?我用谷歌搜索了这个,似乎只有 Fortran 2003 具有将参数传递给可执行文件的功能(这是正确的)?

4

1 回答 1

1

供将来参考,正如上面@High Performance Mark 指出的那样,在 Fortran 2003 中很容易做到这一点。下面的示例代码取自这里并显示了如何:

      PROGRAM test_get_command_argument
        INTEGER :: i
        CHARACTER(len=32) :: arg

        i = 0
        DO
          CALL get_command_argument(i, arg)
          IF (LEN_TRIM(arg) == 0) EXIT

          WRITE (*,*) TRIM(arg)
          i = i+1
        END DO
      END PROGRAM
于 2013-11-09T14:54:49.957 回答