Fortran write statement working on 1 line & not the next

  • Comp Sci
  • Thread starter cpburris
  • Start date
  • Tags
    Fortran Line
In summary: I wouldn't have known what I was searching for! Thank you SO much for the help!In summary, the issue with the write statements not working in the code provided is due to input/output buffering. By not closing the unit connected with the output buffer, the second write statement did not produce any output. Adding a close statement after the second write statement resolves the issue.
  • #1
cpburris
Gold Member
38
4
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 separated by a single line of notes to behave like this.
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
cpburris said:
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 separated 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:
  • #3
Mark44 said:
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!
 
  • #4
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.
 
  • #5
Mark44 said:
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!
 
  • #6
You're welcome! I'm happy to be able to help out.
 
  • #7
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.
 
  • #8
cpburris said:
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).

Since this is a different question, please start a new thread...
And please use the homework template.
 
  • #9
Mark44 said:
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).

Since this is a different question, please start a new thread...

Will do. I just didn't want to start a new thread if it wasn't an appropriate question for the forum. The code is probably around 240 lines when I take out notes. Thanks again.
 

FAQ: Fortran write statement working on 1 line & not the next

1. Why is the Fortran write statement only working on one line but not the next?

This could be due to a variety of reasons. One possible explanation is that there is an error in the code on the line that is not being written to. Another possibility is that there is an issue with the input or output file being used. It is important to carefully review the code and check for any errors or discrepancies.

2. How can I troubleshoot issues with the Fortran write statement?

First, check for any errors in the code itself. Next, ensure that the correct input and output files are being used. It can also be helpful to use a debugger or print statements to track the flow of the write statement and identify any issues.

3. Can the Fortran write statement be used to write to multiple lines at once?

Yes, the Fortran write statement can be used to write to multiple lines at once by using a loop or array. This can be a more efficient way to write to multiple lines rather than using multiple individual write statements.

4. Are there any special considerations when using the Fortran write statement for formatted output?

Yes, when using the Fortran write statement for formatted output, it is important to pay attention to the data types and format specifiers being used. These must match up correctly in order for the write statement to function properly.

5. Can the Fortran write statement be used to write to files other than text files?

Yes, the Fortran write statement can be used to write to files other than text files, such as binary files. However, the syntax and format may differ depending on the type of file being written to. It is important to consult the Fortran documentation for specific guidelines and examples.

Similar threads

Replies
7
Views
1K
Replies
5
Views
1K
Replies
3
Views
4K
Replies
13
Views
2K
Replies
5
Views
1K
Replies
3
Views
2K
Replies
2
Views
8K
Back
Top