Why Won't My Fortran 90 Loop Output to a File?

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
6 replies · 3K views
Taylor_1989
Messages
400
Reaction score
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
 
Physics news on Phys.org
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 ?
 
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
 
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   Reactions: Taylor_1989
@Mark44 thank you I have just corrected my code.
 
I have taken you comment on board. I will endeavour to improve my context when posting. Once again thank you for the quick responses.
 
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   Reactions: Taylor_1989