1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran write statement working on 1 line & not the next

  1. Sep 8, 2016 #1

    cpburris

    User Avatar
    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: Sep 8, 2016
  2. jcsd
  3. Sep 8, 2016 #2

    Mark44

    Staff: Mentor

    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.
    Code (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: Sep 9, 2016
  4. Sep 9, 2016 #3

    cpburris

    User Avatar
    Gold Member

    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!
     
  5. Sep 9, 2016 #4

    Mark44

    Staff: 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.
     
  6. Sep 9, 2016 #5

    cpburris

    User Avatar
    Gold Member

    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!
     
  7. Sep 9, 2016 #6

    Mark44

    Staff: Mentor

    You're welcome! I'm happy to be able to help out.
     
  8. Sep 9, 2016 #7

    cpburris

    User Avatar
    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.
     
  9. Sep 9, 2016 #8

    Mark44

    Staff: Mentor

    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.
     
  10. Sep 9, 2016 #9

    cpburris

    User Avatar
    Gold Member

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Fortran write statement working on 1 line & not the next
Loading...