Fortran 90 loop output to file

In summary, your program will not work because you forgot to open the unit where you were going to write the output.
  • #1
Taylor_1989
402
14
Hi, guys I am very new to programming in fortran90 and I am just experimenting with stuff. I have been trying to get my output of my loop program to a text file. But for some reason it will not work. Could someone please advise.

Code displayed below:
PROGRAM loop

IMPLICIT NONE

integer :: counter

open(10, file='jason.txt')

do counter= 1,10,1

WRITE(*,*) 'Jason', counter
end do

close(10)end program loop
 
Technology news on Phys.org
  • #2
Taylor_1989 said:
... it will not work.
This is not a helpful statement. Does it fail to compile? Does it compile but fail to output what you expect? Does it compile but fail to output anything at all? You see my point ?
 
  • #3
Taylor_1989 said:
Hi, guys I am very new to programming in fortran90 and I am just experimenting with stuff. I have been trying to get my output of my loop program to a text file. But for some reason it will not work. Could someone please advise.

Code displayed below:
Fortran:
PROGRAM loop

IMPLICIT NONE

integer :: counter

open(10, file='jason.txt')

do counter= 1,10,1

   WRITE(*,*) 'Jason', counter
end do

close(10)end program loop
Your write statement needs to write to the unit you opened -- unit 10.
Link to some documentation: http://www.math.hawaii.edu/~hile/fortran/fort7.htm
 
  • #4
Taylor_1989 said:
WRITE(*,*) 'Jason', counter

Change this to "write(10,*) 'Jason', counter".

But like phinds says, "it doesn't work" isn't usually very helpful information.
 
  • Like
Likes Taylor_1989
  • #5
@Mark44 thank you I have just corrected my code.
 
  • #6
I have taken you comment on board. I will endeavour to improve my context when posting. Once again thank you for the quick responses.
 
  • #7
Taylor_1989 said:
I have taken you comment on board. I will endeavour to improve my context when posting.
Good. Saying "for some reason it will not work" really doesn't tell us anything. Fortunately your program was very simple, so several of us were able to spot the problem right away.

For future reference, the more information you give us about the problems you're having, the faster we can find the problem. Useful information is whether the program will compile or not. (If it won't compile, you have syntax errors, usually the easiest kinds of errors to find and fix.)

If the program compiles, but doesn't produce correct output, or even any output at all, that is useful information. The time-honored technique of including extra write statements is useful, especially if you don't have or don't know how to use a debugger.
 
  • Like
Likes Taylor_1989

What is Fortran 90 loop output?

Fortran 90 loop output refers to the process of printing or writing data from a loop in a Fortran 90 program to an external file. This allows for the data to be stored and used for further analysis or processing.

How do I output a Fortran 90 loop to a file?

To output a Fortran 90 loop to a file, you will need to first open the file using the OPEN statement and specify the file name and access mode. Then, within the loop, use the WRITE statement to write the data to the file. Finally, close the file using the CLOSE statement before ending the loop.

Can I output multiple loops to the same file?

Yes, you can output multiple loops to the same file by using the OPEN statement with the POSITION='APPEND' keyword. This will append the data from each loop to the end of the existing file rather than overwriting it.

What is the recommended format for outputting a Fortran 90 loop to a file?

The recommended format for outputting a Fortran 90 loop to a file is using the FORMAT statement to specify the desired layout of the data. This allows for better organization and readability of the data in the file.

How do I handle errors in Fortran 90 loop output to file?

To handle errors in Fortran 90 loop output, you can use the IOSTAT keyword in the WRITE statement to check for any errors during the writing process. You can also use the ERR keyword in the OPEN statement to specify a specific error-handling subroutine.

Similar threads

  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
4
Views
626
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
17
Views
4K
  • Programming and Computer Science
Replies
13
Views
2K
Back
Top