从命令行将参数传递给可执行文件 [英] Passing arguments to executable from command line

查看:45
本文介绍了从命令行将参数传递给可执行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这个程序的输出是:

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

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

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)?

推荐答案

为了将来参考,正如@High Performance Mark 上面指出的,在 Fortran 2003 中很容易做到这一点.下面的示例代码取自 此处 并展示了方法:

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

这篇关于从命令行将参数传递给可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆