【求助】fortran可否生成带有参数的可执行程序
ansys中可以用/syp,myprogr.exe,k1,k2,k3...(ansys命令)这样的命令调用可执行程序,后面k1,k2。。。是可执行程序的输入参数
所以我想是否可以用fortran生成带有参数的可执行程序?
我知道:fortran生成.exe文件没问题,带有参数的function也行。
但是带有参数的.exe可以马?
回复: 【求助】fortran可否生成带有参数的可执行程序
可以的,没有问题。关键是怎么在程序中获取这些参数,有两个函数可以实现这个功能,忘了。回复: 【求助】fortran可否生成带有参数的可执行程序
GETARGRun-Time Subroutine: Returns the specified command-line argument (where the command itself is argument number 0).
Module: USE DFLIB
Syntax
CALL GETARG (n, buffer [, status])
n
(Input) INTEGER(2). Position of the command-line argument to retrieve. The command itself is argument number 0.
buffer
(Output) Character*(*). Command-line argument retrieved.
status
(Optional; output) INTEGER(2). If specified, returns the completion status. If there were no errors, status returns the number of characters in the retrieved command-line argument before truncation or blank-padding. (That is, status is the original number of characters in the command-line argument.) Errors return a value of -1. Errors include specifying an argument position less than 0 or greater than the value returned by NARGS.
GETARG can be used with two or three arguments. If you use module DFLIB.F90 in the \DF98\INCLUDE subdirectory (by including the statement USE DFLIB), you can mix calls to GETARG with two or three arguments. If you do not use DFLIB.F90, you can use either two-argument or three-argument calls to GETARG but only one type of call within a subprogram.
GETARG returns command-line arguments as they were entered. There is no case conversion.
If the command-line argument is shorter than buffer, GETARG pads buffer on the right with blanks. If the argument is longer than buffer, GETARG truncates the argument. If there is an error, GETARG fills buffer with blanks.
Assume a command-line invocation of ANOVA -g -c -a, and that buffer is at least five characters long. The following GETARG statements return the corresponding arguments in buffer:
Compatibility
CONSOLE STANDARD GRAPHICS QUICKWIN GRAPHICS WINDOWS DLL LIB
回复: 【求助】fortran可否生成带有参数的可执行程序
最好把获取参数个数的那个函数也贴出来。回复: 【求助】fortran可否生成带有参数的可执行程序
好的。NARGS
Run-Time Function: Returns the total number of command-line arguments, including the command.
Module: USE DFLIB
Syntax
result = NARGS( )
Results: The result is of type INTEGER(4). The result is the number of command-line arguments, including the command. For example, NARGS returns 4 for the command-line invocation of PROG1 -g -c -a.
Compatibility
CONSOLE STANDARD GRAPHICS QUICKWIN GRAPHICS WINDOWS DLL LIB
Example
USE DFLIB
INTEGER(2) result
result = RUNQQ('myprog', '-c -r')
END
!MYPROG.F90 responds to command switches -r, -c,
!and/or -d
USE DFLIB
INTEGER(4) count, num
INTEGER(2) i, status
CHARACTER(80) buf
REAL r1 / 0.0 /
COMPLEX c1 / (0.0,0.0) /
REAL(8) d1 / 0.0 /
num = 5
count = NARGS( )
DO i = 1, count-1
CALL GETARG(i, buf, status)
IF (buf(2:status) .EQ.'r') THEN
r1 = REAL(num)
WRITE (*,*) 'r1 = ',r1
ELSE IF (buf(2:status) .EQ.'c') THEN
c1 = CMPLX(num)
WRITE (*,*) 'c1 = ', c1
ELSE IF (buf(2:status) .EQ.'d') THEN
d1 = DBLE(num)
WRITE (*,*) 'd1 = ', d1
ELSE
WRITE(*,*) 'Invalid command switch'
EXIT
END IF
END DO
END
IARGC
Portability Function: Returns the index of the last command-line argument.
Module: USE DFPORT
Syntax
result = IARGC( )
Results: The result is of type INTEGER(4). The result is the index of the last command-line argument. For example, IARGC returns 3 for the command-line invocation of PROG1 -g -c -a.
IARGC returns a value that is 1 less than that returned by NARGS. The command is not included in the index count returned by IARGC.
Compatibility
CONSOLE STANDARD GRAPHICS QUICKWIN GRAPHICS WINDOWS DLL LIB
Example
use dfport
integer(4) no_of_arguments
no_of_arguments = IARGC ( )
print *, 'total command line arguments are ', no_of_arguments
回复: 【求助】fortran可否生成带有参数的可执行程序
呵呵,上午看了半天,没看动,现在再回去看看。:)
回复: 【求助】fortran可否生成带有参数的可执行程序
看看我第二个帖子的示例就好使:D补充一句,这是CVF专用的。
回复: 【求助】fortran可否生成带有参数的可执行程序
呵呵,回去一看,发现自己已经看过了,实际上帮助这里块,我还运行了一下,结果还都很正常,只是对函数的作用感觉不是很清楚啊
对于过程还不是很清楚。
问题如下。
getarg这个函数中GETARG (n, buffer ,status)
n表示的是参数的位置个数吗?
buffer应该代表的是从外部传递过来的参数吧?是字符型的?
别的类型,比如整型,双精度,我试了一下,:)果然不可以。
例子中调用函数时,在外部将-r,-c传了进来,但是传的是字符。如果是具体数值呢?可以马?我想将数值,3.5之类的,或者数值变量传入
然后进行一系列的运算啊。
是不是我那里理解错了?昨晚研究了半宿帮助,也没弄明白。
请斑竹再指点以下,呵呵,最好来点中文的,
还有一个更弱的问题,就是定义中我看经常出现INTEGER(2),INTEGER(4),之类的,
我知道是整型的定义,其中的2,4代表啥意思啊?是说变量的长度吗?
回复: 【求助】fortran可否生成带有参数的可执行程序
帮助上说的很清楚,GETARG中的buffer变量,必须是字符型,想获得整型或浮点数,可以将字符串转成其他类型即可,如下段代码。REAL (8) :: TA
CHARACTER(6) :: STRA
STRA = '312.50'
READ (STRA, '(F5.2)') TA
WRITE(*,*) STRA
WRITE(*,*) TA
另外关于integer(2), integer(4)里的2,4指的是数据的kind(不知道怎么翻译),即限定了数据的范围,用HUGE函数即可得到最大值。如下是我得到INTEGER(1), (2), (4), (8)的最大整数:
127327672147483647 9223372036854775807
回复: 【求助】fortran可否生成带有参数的可执行程序
谢谢斑竹我,我知道了:)
回复: 【求助】fortran可否生成带有参数的可执行程序
Fortran中可以不用这样做的!你自习查看一下Fortran 90得书籍,
都会有的。
可以从命令行输入参数,也就是预置参数。
例如:
test.exe a b c
Visual Fortran 得help文档里面有实例得。
回复: 【求助】fortran可否生成带有参数的可执行程序
:?) 手头没有fortran90的书啊,有两本基础的,没有讲这些帮助,无从查起啊
不知道你明白我的用意没有,我要执行下面的命令
my order,test.exe(一个可执行文件),(它的输入参数)a,b,c
你能够给出命令,
或者帮助里怎么查到这个例子呢?
thank u anyway
回复: 【求助】fortran可否生成带有参数的可执行程序
你的参数不是文件名啊?不是也可以。
你在主程序里面加个read语句不就完了?
不知道是不是领会了你的意思?
不是这个意思QQ联系我吧。
151772149
回复: 【求助】fortran可否生成带有参数的可执行程序
关于命令行参数楼上说的应该是文件名的问题(当然适当处理也可以读入非文件名参数),参考如下:1. Specifying /fpscomp:filesfromcmd affects the following Fortran features:
a. The OPEN statement FILE specifier
For example, assume a program OPENTEST contains the following statements:
OPEN(UNIT = 2, FILE = ' ')
OPEN(UNIT = 3, FILE = ' ')
OPEN(UNIT = 4, FILE = ' ')
The following command line assigns the file TEST.DAT to Unit 2, prompts the user for a filename to associate with Unit 3, then prompts again for a filename to associate with Unit 4:
opentest test.dat '' ''
b. Implicit file open statements such as the WRITE, READ, and ENDFILE statements
Unopened files referred to in READ or WRITE statements are opened implicitly as if there had been an OPEN statement with a name specified as all blanks. The name is read from the command line.
WRITE(UNIT = 8, FMT='(2I5)') int1, int2 ! Where "8" has not been
! explicitly associated with a file.
2. If you specified the /fpscomp:filesfromcmd option (Compatibility category in Project Settings, Fortran tab), the command line that executes the program can also include additional filenames to satisfy OPEN statements in your program in which the filename field (FILE specifier) has been left blank. The first filename on the command line is used for the first such OPEN statement executed, the second filename for the second OPEN statement, and so on. In the visual development environment, you can provide these filenames in the Project menu Settings item, Debug tab, in the Program Arguments text box.
Each filename on the command line (or in a visual development environment dialog box) must be separated from the names around it by one or more spaces or tab characters. You can enclose each name in double quotation marks ("filename"), but this is not required unless the argument contains spaces or tabs. A null argument consists of an empty set of double quotation marks with no filename enclosed ("").
The following example runs the program MYPROG.EXE from the console:
MYPROG "" OUTPUT.DAT
Because the first filename argument is null, the first OPEN statement with a blank filename field produces the following message:
File name missing or blank - please enter file name<R>
UNIT number?
The number is the unit number specified in the OPEN statement. The filename OUTPUT.DAT is used for the second such OPEN statement executed. If additional OPEN statements with blank filename fields are executed, you will be prompted for more filenames. Programs built with the QuickWin library prompt for a file to open by presenting a dialog box in which you can browse for the file or type in the name of a new file to be created.
3. Instead of using the /fpscomp:filesfromcmd option, you can:
a. Call the GETARG library routine to return the specified command-line argument. To execute the program in the visual development environment, provide the command-line arguments to be passed to the program in the Project menu Settings item, Debug tab, in the Program Arguments text box.
b. Call the GetOpenFileName Win32 routine to request the file name using a dialog box. For an example of how to call the GetOpenFileName routine, see the Visual Fortran Sample GetOpenFileName in ...Df98\Samples\Advanced\Win32\GetOpenFileName.
回复: 【求助】fortran可否生成带有参数的可执行程序
偶的英语也是一般般;),偶刚上大学的时候英语可是不及格的。看这个东西关键是耐心,其实大家的英文都没有问题(比我强多了),技术文章语法都比较简单的。耐心都下来没问题的。学习什么编程语言并不十分重要,计算机只是一个工具,是用来解决实际问题的。搞工程计算的,如果行业内大家都用FORTRAN那就学FORTRAN好了,如果需要学VC++就学(如果将之当作c++语言的开发平台做计算还可以考虑,如果仅仅是为了进行界面编程意义就不大了,毕竟大家不是纯粹的计算机专业的),学fortran价值大不大取决于你的今后工作方向,有需要价值就大。不过编程挺锻炼能力的。
Re:回复: 【求助】fortran可否生成带有参数的可执行程序
例子:c ModifyKAAX(InputFCHFile,OutputFCHFile,modify_KAAX_coefficient)
c
character*256 InputFCHFile
character*256 OutputFCHFile
character modify_KAAX_coefficient*8
if ( NARGS() .ne. 4 ) stop 'Incorrect number of arguments.'
c NARGS() 判断输入参数个数,并作错误判断
call GETARG(1, InputFCHFile)
call GETARG(2, OutputFCHFile)
call GETARG(3, modify_KAAX_coefficient)
c GETARG依次获取参数,并赋给相应值
open (7, file = InputFCHFile, status='old')
open (8, file = OutputFCHFile, status='unknown')
c ........
Re:【求助】fortran可否生成带有参数的可执行程序
systemqq,getarg等几个函数在运用可执行程序中常用。顺便说一下,参看彭国论的FORTRAN 95程序设计,比较适合工程人员使用。
页:
[1]