New Fortran elements, at a glance
Summary
Guide To Learn
Let’s redirect output to standard output and error channels into their respective files from within the Fortran program. Why would you want to do this? For some applications that run for a long time, such as large weather and ocean prediction models that can run for days, it’s useful to write standard output and error […]
This section contains the solution to the exercise in this chapter. Skip ahead if you haven’t worked through the exercises yet.
In our quick-note app, we haven’t had an explicit need to close the file because we’re prompting the user for input and writing to the file in an infinite loop. The only way for the program to quit is by the user pressing Ctrl+C. However, if any of the I/O statements encounters an error (such […]
With the upgrade to the open statement that allows our program to open an existing file in the append mode, we can now continue writing notes to an old file. This is a useful feature, but it assumes that the user would want to continue writing. There are situations where I’d want to start writing from the […]
Sooner or later, you’ll run across files that are meant to be used as read-only; for example, important satellite data that shouldn’t be overwritten. Similarly, some files will be useful only in write-only mode, such as the quick notes that we’ve been working on in this section. Fortran allows you to open a file in […]
Our little note-taking app is simple and neat and does what it promised. However, one of the first issues we may run into is that if we specify an existing file to write our notes into, the program will overwrite the previous contents of the file with our new notes. The following listing shows an […]
OK, so we’ve parsed the command line for a filename as the argument, and we’ve opened the file as requested by the user. Let’s write some notes to it. To do this, we’ll need to parse user input from the keyboard, and write the data to the file on each new line–that is, every time […]
Once we have the desired filename stored in the filename variable, our next step is to open the file for writing. Opening a file means creating a connection between the program and some file on disk. We can do this using the open statement: ❶ Opens a file with a given filename, assigning its I/O unit to fileunit This is one of the […]
The first step involves getting the filename as a command-line argument, opening the file for writing, and allowing the user to write into it. To implement this is relatively straightforward but will require quite a few new language elements. Let’s take a look at the complete program first, as shown in the following listing. Listing […]