Passing arguments to executable from command line

风流意气都作罢 提交于 2019-12-14 02:15:42

问题


I'm trying to pass arguments to a Fortran executable from the command line. A sample program that achieves this in C is (taken from here):

#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;
}

The output of this program is:

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

My question now is, how would I code this program (and therefore this functionality) in Fortran? I have googled this, and it seems that only Fortran 2003 has the functionality of passing arguments to executables (is this correct)?


回答1:


For future reference, as @High Performance Mark points out above, it is quite easy to do this in Fortran 2003. The below example code is taken from here and shows how:

      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


来源:https://stackoverflow.com/questions/19873497/passing-arguments-to-executable-from-command-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!