FORTRAN main program parameters and simple file operations

Source: Internet
Author: User
1. in C language, the main function main () can contain parameters, and only two parameters can be included.
  main(int argc, char * argv[]){}

Here, if you run the program in cmd, the program file name itself is also a parameter, so argc = number of input parameters + 1. argv [0] stores the program file name.

 

2. In FORTRAN, the main function has no parameters. If you want to execute the Fortran Program in the command line and PASS Parameters to Fortran, you need to call the corresponding function in the program.
agrc=iargc()

Returns the number of command line parameters.

  
call getarg(i,charstring)
Read the I parameter of the command line and store it in charstring. The command itself is the 0th parameter.
 1 Example:  2     ! procedure has 2 arguments, one is input file, other is output file 3 PROGRAM MAIN 4     IMPLICIT NONE 5     INTEGER argc 6     character*60 FILEIN,FILEOUT 7      8     IF(nargin==0) THEN 9         FILEIN      =‘FILE.IN‘   !default10     ELSEIF(nargin==1) THEN11         CALL getarg(1, FILEIN);         !Set input file only12     ELSE13         CALL getarg(1, FILEIN);         !Set both input and output files14         CALL getarg(2, FILEOUT);15     ENDIF16 17     <Other code>18     19     stop20 END MAIN

 

3. For Versions later than Fortran 2003, use the following function to obtain parameters:

Function 1:Command_argument_count ()-Get number of command line arguments

This is a function with a return value.

Example:           program test_command_argument_count               integer :: count               count = command_argument_count()               print *, count           end program test_command_argument_count 

 

Subroutine 2: Get_command_argumentSimilar Getarg () subroutineUsage: Call get_command_argument (number [, value, length, status]) Explanation: number is the number of parameters obtained, and value is the corresponding value; (Let number = 0 get the name of the executable program. If the number of input parameters is smaller than the number, the result is blank .) Length is the length of the Number Parameter; status is the status after this parameter is obtained (if this parameter is incorrect, a positive number is returned; if the value is a truncated parameter, -1 is returned, and 0 is returned in other cases)
Return value:The return value is an INTEGER of default kind.Example:          program test_command_argument_count              integer :: count              count = command_argument_count()              print *, count          end program test_command_argument_count

 

 

Subroutine 3:Get_command-Get the entire command line

Description: Retrieve the entire command line that was used to invoke the program. Standard: Fortran 2003 and later Class: Subroutine Syntax: CALL GET_COMMAND([COMMAND, LENGTH, STATUS]) Arguments: COMMAND (Optional) shall be of type CHARACTER and of default kind. LENGTH (Optional) Shall be of type INTEGER and of default kind. STATUS (Optional) Shall be of type INTEGER and of default kind.
Return value:If COMMAND is present, stores the entire command line that was used to invoke the program in COMMAND. If LENGTH is present, it is assigned the length of the command line. If STATUS is present, it is assigned 0 upon success of the command, -1 if COMMAND is too short to store the command line, or a positive value in case of an error.Example:          PROGRAM test_get_command            CHARACTER(len=255) :: cmd            CALL get_command(cmd)            WRITE (*,*) TRIM(cmd)          END PROGRAM

 

4. The FORTRAN program locates the file pointer to the end

You can use the rewind () function to adjust the file pointer to the file header.

Adjust the file pointer to the end of the file:It can be used to determine whether the file is complete: set the end mark at the end of the file. If a file is traversed once, no "End mark" is found at the end, indicating that the file is incomplete.

character bufferdo     read(1,"(A)",iostat=stat1) buffer    if(stat1/=0) exit !when file end ,skip to cycleenddo

 

FORTRAN main program parameters and simple file operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.