Fortran 90: Alternative to flush() for f90 Compiler

  • Fortran
  • Thread starter natski
  • Start date
  • Tags
    Fortran
In summary, the conversation discusses the issue of calling the flush command in Fortran 90 and finding a replacement for it in f90. One suggestion is to close and immediately re-open the file, but it may not work if the file was opened with the append attribute. Another suggestion is to use the Fortran version of flush or determine the current file position and try to find a replacement for flush. Testing is recommended to confirm the best solution.
  • #1
natski
267
2
Hi all,

I've recently obtained some code in Fortran 90 that was compiled with gfortran. I am compiling with f90 and one of the called statements is not recognised.

I have call flush(1) where 1 is the filename that the code is flushing.

Since flush is not a part of f90, is there an alternative statement which will do the same job in f90?

Thanks,

Natski
 
Technology news on Phys.org
  • #2
Without looking too deeply into whether there is a direct replacement, one method that should achieve the same result would be to close the file and then immediately re-open it.
 
  • #3
Hmm, the open command has position='append', so presumably this is why one cannot simply close then open?
 
  • #4
natski said:
Hmm, the open command has position='append', so presumably this is why one cannot simply close then open?

Yes close and re-open will definitely have the effect of flushing any buffers but as you point out it will be necessary to be able to get back to the same point in the file somehow.
 
  • #5
Flush is a C command really, but it seems that there must be a Fortran version of this command, without opening and closing?
 
  • #6
Can you determine (by reading the code) exactly where the current file position is when it's flushed? I mean if it's somewhere convenient like right at the beginning or end then it would be easy enough to just "close/re-open/reposition", but if it's some arbitraty position then yes it would be much better to try and find a replacement for flush.
 
  • #7
If the file was opened with the append attribute, closing and reopening it with the same append attribute should get the file pointer to point to the same place, as long as we are dealing with a sequential text file and there are no concurrent processes.
Do a little test to confirm it.

Also, some f90 dialects support flush without the unit number, as in flush().
 

Related to Fortran 90: Alternative to flush() for f90 Compiler

What is Fortran 90?

Fortran 90 is a high-level programming language commonly used in scientific and engineering applications. It is an extension of the original Fortran language and was released in 1991.

What is the purpose of the "flush()" function in Fortran 90?

The "flush()" function in Fortran 90 is used to force the output of data to be written to a file or other output device immediately, rather than being buffered and written at a later time.

Why is the "flush()" function being considered as an alternative in Fortran 90?

The "flush()" function can be problematic in certain situations, such as when writing to remote files or when working with large amounts of data. Therefore, Fortran 90 offers alternative methods for controlling the output of data.

What are some alternative methods for controlling output in Fortran 90?

Fortran 90 offers several built-in functions, such as "newunit()" and "open()", for managing output to files. Additionally, the language allows for the use of formatted output statements, which can specify when and how data is written to a file.

How can I determine which method is best for managing output in my Fortran 90 code?

The best method for managing output in Fortran 90 will depend on the specific needs of your program. It is recommended to consult the Fortran 90 documentation or seek guidance from experienced Fortran programmers to determine the most appropriate approach for your code.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
34
Views
3K
Replies
6
Views
1K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
21
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
8K
Back
Top