# Fortran write statement working on 1 line & not the next

• Comp Sci
Gold Member
Not really a question suited for the template...maybe shouldn't put this in Homework section, but it's for a class.

My write files were being weird so I started some investigating. Below is an excerpt from the code.

58 OPEN(unit=33,file='random2.txt',status='unknown')
59 write(33,*)'intitials5'
60 ! Write Statements Working.
61 write(33,*)'initials4'
62 ! Write Statements Not Working.

I've included line numbers for reference. Directly copy and pasted from my code. Compiled using Cygwin with gfortran/GCC. No errors compiling. Written in Fortran 90. The write statement on line 59 works just fine, the write statement on line 61 does absolutely nothing. At least the first time I run the code the first write statement works. If I try and run it a second time, neither write statement works and I end up with an empty file. However, if I run this program,

program Test
OPEN(unit=33,file='random2.txt',status='unknown')
write(33,*)'Test'
end program Test

then run my main program again, the first write statement works again, but not the second.

I am so completely confused. I can provide more of the code if need be, but I can't see how other parts of the code could cause write statements seperated by a single line of notes to behave like this.

Last edited by a moderator:

Related Engineering and Comp Sci Homework Help News on Phys.org
Mark44
Mentor
Not really a question suited for the template...maybe shouldn't put this in Homework section, but it's for a class.

My write files were being weird so I started some investigating. Below is an excerpt from the code.

58 OPEN(unit=33,file='random2.txt',status='unknown')
59 write(33,*)'intitials5'
60 ! Write Statements Working.
61 write(33,*)'initials4'
62 ! Write Statements Not Working.

I've included line numbers for reference. Directly copy and pasted from my code. Compiled using Cygwin with gfortran/GCC. No errors compiling. Written in Fortran 90. The write statement on line 59 works just fine, the write statement on line 61 does absolutely nothing. At least the first time I run the code the first write statement works. If I try and run it a second time, neither write statement works and I end up with an empty file. However, if I run this program,

program Test
OPEN(unit=33,file='random2.txt',status='unknown')
write(33,*)'Test'
end program Test

then run my main program again, the first write statement works again, but not the second.

I am so completely confused. I can provide more of the code if need be, but I can't see how other parts of the code could cause write statements seperated by a single line of notes to behave like this.
Here's what I think is happening. In the first program snippet, you open unit 33, write to it once, then write to it again. The second write doesn't seem to produce anything. I suspect that what is happening is that output is buffered, and your program terminates without the output buffer being sent to the file.

See if this makes a difference.
Fortran:
program ABC
open(unit=33, file='random2.txt', status='new')
write(33, *) 'Some text'
write(33, *) 'Some other text'
close(33)
end program ABC
I don't have a Fortran compiler to test this on, but I believe that closing the unit will cause the output buffer to be flushed, so you should get two lines of text output.

Last edited:
Gold Member
Here's what I think is happening. In the first program snippet, you open unit 33, write to it once, then write to it again. The second write doesn't seem to produce anything. I suspect that what is happening is that output is buffered, and your program terminates without the output buffer being sent to the file.

See if this makes a difference.
Fortran:
program ABC
open(unit=33, file='random2.txt', status='new')
write(33, *) 'Some text'
write(33, *) 'Some other text'
close(33)
end program ABC
I don't have a Fortran compiler to test this on, but I believe that closing the unit will cause the output buffer to be flushed, so you should get two lines of text output.
Interesting. I don't know anything about output buffering, I'll have to look into it. From the limited info I gathered about I/O buffering quick in these last few minutes before bed it does seem a possible cause. I tried the code you suggested and it worked. I will have to investigate more tomorrow. Thanks for the suggestion!

Mark44
Mentor
The takeway here should be that if you open a file for reading (input) or writing (output), when you're done with the file you should close it. Input/output buffering is done for the sake of efficiency -- it's more efficient to send or receive a block of characters than getting them or sending them one at a time.

An input or output buffer is simply a block of memory that the program uses as a temporary holding area. In your first program, the second line of output caused the text to go into the output buffer, but since you didn't close the unit connected with your output buffer, the text in the buffer didn't get written to the file.

As I said, I don't have a Fortran compiler any more, so I can't verify what I'm saying here, but I'm reasonably sure this is what was happening with your program.

Gold Member
The takeway here should be that if you open a file for reading (input) or writing (output), when you're done with the file you should close it. Input/output buffering is done for the sake of efficiency -- it's more efficient to send or receive a block of characters than getting them or sending them one at a time.

An input or output buffer is simply a block of memory that the program uses as a temporary holding area. In your first program, the second line of output caused the text to go into the output buffer, but since you didn't close the unit connected with your output buffer, the text in the buffer didn't get written to the file.

As I said, I don't have a Fortran compiler any more, so I can't verify what I'm saying here, but I'm reasonably sure this is what was happening with your program.
I added a quick close statement and looks like you were right! So happy! That's why I love this forum, I could have probably searched for hours and hours trying to figure out what was going on and still probably just have ended up rewriting the code to eliminate the write statements. Many thanks!

Mark44
Mentor
You're welcome! I'm happy to be able to help out.

Gold Member
I have kind of a longer question about my code in general, and I guess I want to know if you think that it's a question I could ask here. The code solves for particle trajectories. I have a file with initial conditions for 24 particles, and I have a parameter which just tells my program how many of those particles I want to run the program for. If I set the parameter to '22', telling it to read in the first 22 initial conditions and run for only those particles, I get completely different results for the particles then if I set it to '24'. All changing the parameter to 24 should do is run 2 more particles giving me 2 additional sets of data. It shouldn't affect the calculation of the particle dynamics is any way. I'd probably have to post my entire code and some results so I don't know if that is too much the forum or not.

Mark44
Mentor
I have kind of a longer question about my code in general, and I guess I want to know if you think that it's a question I could ask here. The code solves for particle trajectories. I have a file with initial conditions for 24 particles, and I have a parameter which just tells my program how many of those particles I want to run the program for. If I set the parameter to '22', telling it to read in the first 22 initial conditions and run for only those particles, I get completely different results for the particles then if I set it to '24'. All changing the parameter to 24 should do is run 2 more particles giving me 2 additional sets of data. It shouldn't affect the calculation of the particle dynamics is any way. I'd probably have to post my entire code and some results so I don't know if that is too much the forum or not.
How big is the code (i.e., how many lines, approximately)?

An alternative to showing all of the code if it is fairly large, is to show us the data file you're reading in, and the section of code that does input. It's possible that your read statements aren't picking up the data correctly. Also, if you are reading from an input file, you.need to close the unit when you are finished reading from it, for the same reason as before (input buffer not being flushed).

And please use the homework template.

Gold Member
How big is the code (i.e., how many lines, approximately)?

An alternative to showing all of the code if it is fairly large, is to show us the data file you're reading in, and the section of code that does input. It's possible that your read statements aren't picking up the data correctly. Also, if you are reading from an input file, you.need to close the unit when you are finished reading from it, for the same reason as before (input buffer not being flushed).