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

AI Thread Summary
The discussion centers on a beginner's attempt to write output from a Fortran 90 program to a text file, specifically 'jason.txt'. The user initially encounters issues with their code, which compiles but does not produce the expected output. The main problem identified is that the WRITE statement is incorrectly using the default output unit instead of the file unit that was opened (unit 10). The correct syntax for the WRITE statement should be "write(10,*) 'Jason', counter". Participants emphasize the importance of providing detailed information about issues, such as whether the program compiles or produces output, to facilitate quicker troubleshooting. Suggestions include using additional WRITE statements for debugging purposes if a debugger is unavailable.
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
 
Technology 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 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 Taylor_1989

Similar threads

Replies
12
Views
3K
Replies
5
Views
5K
Replies
4
Views
2K
Replies
8
Views
4K
Replies
3
Views
3K
Replies
13
Views
3K
Back
Top