MC, 2025
Ilustracja do artykułu: Mastering Fortran Write: A Comprehensive Guide with Examples

Mastering Fortran Write: A Comprehensive Guide with Examples

Fortran is a powerful language, widely used for scientific computing, simulations, and numerical analysis. One of the most essential features of Fortran is its ability to handle input and output efficiently. In this article, we will focus on the Fortran WRITE statement, a fundamental part of this process. Whether you're just starting with Fortran or looking to enhance your skills, understanding how to use the WRITE statement will be invaluable.

What is the Fortran WRITE Statement?

The Fortran WRITE statement is used for outputting data to various devices, including the screen, files, or even printers. In simpler terms, it's how you "write" information in your Fortran programs. It allows you to display or save the results of calculations or any other information that needs to be presented to the user or stored for future reference.

WRITE can be used in multiple contexts, making it a versatile tool for any Fortran programmer. From simple screen outputs to writing complex data structures to files, the WRITE statement handles it all. We will take a closer look at its syntax, usage, and examples so you can make the most of this powerful command.

Basic Syntax of the WRITE Statement

Before diving into examples, let's first understand the basic syntax of the WRITE statement. Here's a simple structure:

WRITE(unit, format) list_of_variables

In this syntax:

  • unit: This specifies the unit number. In Fortran, a unit number is a reference to a specific I/O device (such as the screen or a file).
  • format: This is a specification of how the data should be displayed. You can choose a pre-defined format or create your own.
  • list_of_variables: This is the data you want to print or write to the specified device.

Now that we understand the basics of the syntax, let’s explore some common use cases.

Writing to the Screen

The most common use of the WRITE statement is to display information on the screen. This is especially useful when you want to show the results of calculations or debugging information. Let’s look at a simple example where we print a message to the screen:

program hello
  write(*,*) 'Hello, world!'
end program hello

In this example:

  • write(*,*) tells Fortran to output to the default screen (denoted by '*').
  • 'Hello, world!' is the string we want to print.

This code will print "Hello, world!" to the screen when executed. The *,* format means that we are using a default output format and the default unit (the console).

Writing to a File

Writing to a file is another common use of the WRITE statement in Fortran. This allows you to store the output of your program for later analysis. Let’s see an example of how to write to a file:

program write_to_file
  integer :: unit
  open(unit=10, file='output.txt', status='replace')
  write(unit=10,*) 'This is written to a file.'
  close(unit=10)
end program write_to_file

In this example:

  • open(unit=10, file='output.txt', status='replace') opens the file 'output.txt' for writing, using unit 10.
  • write(unit=10,*) writes the string to the file. Here, unit 10 refers to the file we just opened.
  • close(unit=10) closes the file after the data has been written.

This code will create a file named 'output.txt' and write "This is written to a file." to it. If the file already exists, the status='replace' option will overwrite the file.

Formatted Output

In some cases, you may want to control how the data is displayed, such as specifying the number of decimal places or the width of the output. This can be done using a formatted output, where you define the format explicitly. Here’s an example of how to use formatted output:

program formatted_output
  real :: pi = 3.14159
  write(*,'(F6.2)') pi
end program formatted_output

In this example:

  • '(F6.2)' specifies the format. F6.2 means a floating-point number with a width of 6 characters and 2 decimal places.
  • pi is the value being written, and the output will be displayed as ' 3.14' (note the spacing to fit the width specification).

Formatted output is extremely helpful when dealing with numerical data, as it lets you present the data in a cleaner and more readable format.

Writing Multiple Variables

You can also write multiple variables in one statement. For instance, if you want to display multiple results at once, you can list all the variables in the WRITE statement:

program multiple_variables
  integer :: a = 5, b = 10
  real :: result = 25.6
  write(*,*) 'The values are: ', a, b, result
end program multiple_variables

This will output:

The values are: 5 10 25.6

By simply separating the variables with commas in the write(*,*) statement, Fortran will print them sequentially. This makes it easy to display multiple values in a single line.

Write with Custom Formatting

If you need more control over the appearance of your output, you can specify a custom format in the WRITE statement. Let’s look at an example where we create a custom format for printing:

program custom_format
  integer :: x = 123
  real :: y = 456.789
  write(*, '(A, I5, F6.2)') 'Value of x: ', x, y
end program custom_format

Here’s what’s happening:

  • '(A, I5, F6.2)' is the format. A is for a string, I5 formats the integer with a width of 5 characters, and F6.2 formats the float with 2 decimal places.
  • 'Value of x: ' is the string being printed, followed by the formatted values of x and y.

This results in the following output:

Value of x:   123 456.79

Custom formats allow you to format your output to suit your needs, making it cleaner and more professional.

Conclusion

The Fortran WRITE statement is an essential tool for outputting data in Fortran programs. From simple screen outputs to writing data to files with precise formatting, the WRITE statement provides flexibility and control over how data is presented. By understanding its syntax and exploring different examples, you can enhance your Fortran programs with clear, well-formatted output that is easy to interpret and use.

We hope this guide has helped you gain a better understanding of how to use the Fortran WRITE statement effectively. Whether you’re writing data to the screen or saving it to a file, the possibilities are endless!

Komentarze (0) - Nikt jeszcze nie komentował - bądź pierwszy!

Imię:
Treść: