How To Run A Fortran Program In Command Prompt

Force Fortran Compiler and Editor

  1. Preparation: Create a specific folder in your Personal Storage area (i.e., your I‑drive) to contain all of your Fortran files. Double-click My Computer and double-click the icon for your I-drive. Right-click in the display of the 'contents' of your I-drive and select New and then select Folder. A new folder is created and awaits a new name. Enter an appropriate name, like my_stuff and then press ENTER. All of the Fortran-related files should be created in this new folder.
  2. Launch the Force Fortran compiler and editor with Start > Programs > Force 2.0 > Force 2.0.
  3. The Force environment is divided into several different panels. The Files panel is at the top left and is used to indicate the files that are being used. (If this panel is not visible, use View > Files; this panel can be removed by clicking the close button at the top right corner of the panel.) The larger panel to the right is for entering the lines of a source program. Spanning the fill width of the screen beneath the Files and Source program panels is the Messages panel used to display error messages when a source program is compiles. (If this panel is not visible, use View > Messages; this panel can be removed by clicking the close button at the top left corner of the panel.) Beneath the Messages panel, is the Output panel, used to display program output when the program is successfully compiles (and linked) and executed. (If this panel is not visible, use View > Output; this panel can be removed by clicking the close button at the top left corner of the panel.)
  4. In the Source program panel, enter the lines of the source program. A vertical line marks the beginning of column 7; similarly, another vertical line marks the beginning of line 73. Force 'remembers' the amount of indentation of the previous line by automatically indenting the next line. The continuation of a previous line must have a non-blank, non-zero character in column 6. Comments begun with the exclamation point '!' are rendered in gray color and in italics as are full-line comments begun with the asterisk '*'. However, comments begin with the letter 'c' in column 1 are not.
  5. Save a source program by clicking the Save tool () or the Save All tool (). Source program files are saved with the .f suffix. Force will automatically save a source program before it tries to compile it.
  6. To compile (and link and begin execution) the visible source program, click the Compile (and link and execute/run) tool (). If all goes well, a Command Prompt window will appear to display the output of the program. If there are errors, Force will display appropriate messages in the Messages panel. Close the this execution window to return to Force.

'Manual ' execution with input or output files

If the program is to accept input data from an existing file or to preserve the output in a file, you must open a Command-Prompt window and execute the compiled program 'manually'.

  • Compile and link the source program is indicated in the procedure outlined above. When the execution window appears, abort the execution by using CTRL-C (i.e., hold down the CTRL key and press the C-key). Then close the execution window. You can just compiler the current source program by using Run > Compile.
  • Open a Command-Prompt window: Start > Programs > Accessories > Command Prompt.
  • Navigate to the appropriate folder for the executable file (which will have a suffix of .EXE). This executable file will be in the folder that you created to contain all of your Fortran files, like my_stuff.

Some DOC commands

On Windows systems, the supplied Fortran command-line window in the Intel ® Fortran program folder, in which the appropriate environment variables in ifortvars.bat are preset. The ifort command invokes a driver program that is the actual user interface to the compiler and linker. If you do enough Fortran programming, sooner or later you will err and create and run a program that never stops. In such a situation, type 'Control-C' to interrupt the execution of the program. Now that we have discussed the basic nuts and bolts of creating and running a Fortran program, we discuss some terminology and commands.

The following commands will be helpful; each command must be typed explicitly and concluded with the ENTER key.

To change from the C-drive to the I-drive, use the command

I:

To see what files are currently available, use the

dir

command. The names of the files are in the last column of the resulting listing. Subordinate directories/folders have <DIR> in the third column.

To 'move into' a subordinate folder (or directory) [e.g., my_stuff], use the command

chdirmy_stuff

To move to the next higher folder (the 'parent' folder), use the command

chdir .

(i.e., move to the double-dot (with no space between the dots) folder which represents the 'parent' of the current folder).

Assuming that the text file named mfp.dat contains the data to be used by the executable file mfp.exe (i.e., the executable file for 'My First Program') and assuming that the output from the program should be preserved in a text file named mfp.out, use the following Command-Prompt command

mfp <mfp.dat >mfp.out

The item <mfp.dat re-directs the input from the keyboard to the existing file named mfp.dat as though its contents were manually entered on the keyboard; each line is one set of data which would be entered manually from the keyboard and concluded by the ENTER key. Omitting this input-redirection item means the executing program will expect the input data to come from the keyboard.

The item >mpf.out re-directs the output from the screen to a file named mfp.out, creating it in the process. Omitting this item means the executing program will send its output to the screen. The order of these two re-direction items is not important. Either or both re-direction items may be omitted.

Input/Output

Unit numbers may range from 1 to 99, inclusive. (Unit zero is used for outputting error messages.) If n is the value of a unit, then the program reads from or writes to a file named fort.n unless the file is explicitly opened with an OPEN statement. However, unit 5 is normally used as standard (or default) input and unit 6 is used as standard output unless an OPEN statement is used to override that association.

Normally, the executing program will create an output file in the same directory/folder as the executable file (i.e., the file with the .EXE suffix). Input files are assume to be in the same directory/folder as the executable file. The OPEN statement can be used to open files in different directories/folders where the path to the desired file must be specified explicitly.

If a program is executed and produces an output file and the program is executed a second time (with the output file now pre-existing), the program will abort and produce an error message indicating that the program cannot create the output file. Either the user must first delete the output file before executing the program or the program itself must detect the existence of the output file and delete it. The following source program creates an output file named test.out but checks to see if the file pre-exists; if it doe pre-exist, the output file is deleted and then re-created.

This program will create an output file named test.out in the same directory/folder as the executable file, probably named testfile.exe. Modifying the source program (to change the test that is output) and re-compiling and re-executing it will re-produce the output file (with different contents) without the programmer/user deleting the output file between executions.

Carriage Control

In formatted output, the first character of each line is the carriage control character (i.e., ccc) which controls the movement of the paper in the printer as indicated in the following table:

CCC

Description

space

current line is printed on the next line of the output

+

current line is printed on the same line as the previous output;
i.e., the line 'overprints' the previously output one

0

(zero) a blank line is produced before the current line is output;
hence, double spacing

1

current line is output at the beginning of the next physical page

Most printers are not capable of detecting and interpreting the ccc so they merely print it like any other character. However, you can convert the Fortran output file to an HTML file and view it in an Internet browser (like Internet Explorer or Netscape Navigator); you can then print it through that browser. The program f2html converts a standard Fortran output file to an HTML text file suitable for viewing and printing. In a Command Prompt (i.e., DOS) window, enter the command

where project.out is the Fortran output file and project.htm is the HTML equivalent. Since HTML files cannot completely control the placement of individual lines on the screen or divide the image into pages, the following accommodations are made for f2html:

  • a new page (i.e., ccc of 1) is interpreted as a horizontal rule (or line) across the page.
  • overstriking (i.e., ccc of +) is interpreted as bolding of the original line. For example, the two Fortran output lines

this is fun!
+ fun

would be displayed as

this is fun!


Hence, the word 'fun' is bolded and the introductory blank used as the ccc is not part of the rendered result. The f2html program can accommodate Fortran output files that are approximately 80 characters 'wide'. To display files that are up to 140 characters in width, use the companion program f2htmlw (where the 'w' stands for 'wide'). This latter program is used in exactly the same matter as the f2html program. The resulting file from f2htmlw must be printed in landscape mode.

Compile a program of more than one file

If a complete source program is divided into two or more separate file:

  1. Open all the pertinent files, using the Open tool () to retrieve each one. (Note that the down-arrow will list the files opened recently, but may not be opened now.) Note that all of the files should be in the same directory/folder.


  2. In the Files panel, click the file that contains the main program.


  3. Use Run > Compilation Options. In the Additional Options section and in the Options slot, enter the names of the additional files--including the suffix and separate them from each other by at least one space. Click OK.


  4. Click the Compile tool.


  5. If there are no syntax errors, the complete executable program will begin execution as though all of the source program parts were in in a single file. (The corresponding executable file--with .EXE suffix--can be used later in a DOS/Command Prompt window to associate external input or output files as was indicated earlier.)


Compile and NOT link a single source file

To just compile a single source program file (which may contain a main program or just one subprogram)


  1. Open the appropriate file so that its contents are visible in the source program panel.


  2. Use Run > Compilation Options.


  3. In the Additional Options section and in the Options slot, enter -c -o xxx.exe (i.e., hyphen-C, space, hyphen-O [letter casing does make a difference], space, filename, where xxx is the desired primary name of the file to be created and .EXE is the necessary suffix and click OK. (In the specified option, the -c item indicates to just compile and not also link the associated program unit; the -o item indicates the desired name of the resulting object file. This primary part of the filename must be different from the primary name of the complete executable program when it is assembled) .


  4. Use Run > Compile. Then click OK. The component will be compiled to produce the file xxx.EXE as indicated in the previous step. However, be aware that this file is not an executable file as would be indicated by the file suffix. Strictly speaking, it is an object file would normally have an .O suffix to indicate that.


  5. The resulting non-text/binary file (named xxx.EXE) will be created in the same directory/folder a the source program file but will not appear in the Files panel. Do not try to open the file as such an action will not render the contents for human eyes and will damage the file contents. The resulting compiled file can be linked together with other needed program pats as described in the previous section.

To create a program in the Code::Blocks C integrated development environment, you must build the project. This single step does several things. If you’ve already started your first project, ex0101, and it’s open and displayed in Code::Blocks, you’re ready to build. Heed these steps:

  1. Ensure that the project you want to build is activated in the Management window.

    Activated projects appear in bold text. If you have more than one project shown in the Projects window, activate the one you want to work with by right-clicking the project name (by the Code::Blocks icon) and choosing the Activate Project command.

  2. Choose Build→Build from the menu.

    The Build Log tab in the Logs part of the window displays the results of building the project. You see a few lines of text.

Because you didn’t mess with the source code skeleton, the project compiles with no errors; you see the text within the summary that indicates zero errors and zero warnings. Good. When errors do appear, which is often, you go about fixing them. The error messages help in that regard.

Building a project is only half the job. The other half is running the project, which means executing the completed program from within the IDE.

To run the current project, choose Build→Run from the menu. You see the terminal window appear, listing the program’s output, plus some superfluous text.

Self

Press the Enter key to close the command prompt window.

And now, for the shortcut: You can build and run a project using a single command: Choose Build→Build and Run.

  • The keyboard shortcuts for Build, Run, and Build and Run are Ctrl+F9, Ctrl+F10, and F9, respectively. No need to memorize those shortcuts — they’re listed on the menu.

  • Command buttons to build and run projects are found on the Compiler toolbar. You’ll find a yellow gear icon for Build, a green arrow for Run, and a combination of the two for the Build and Run command.

  • The program output appears in the top part of the command prompt window. The last two lines are generated by the IDE when the program is run.

    The text shows a value returned from the program to the operating system, a zero, and how long the program took to run (9 milliseconds). The Press any key to continue prompt means that you can press the Enter key to close the window.