Fortran: Writes to screen, not to file

In summary, the code writes to file after the entire program has completed, but only after the flush command is used.
  • #1
gluons
15
0
I've never had this issue before. My code will create the file and also output anything to terminal. But if I change the output from terminal to file, it does nothing. For example, this line

write(*,*) "ist1, ist2 =",ist1,ist2

outputs my two indices in every loop iteration. But if I add or replace it with this line,

write(2,*) "ist1, ist2 =",ist1,ist2

nothing happens. I have tried changing the filename, the unit number, what I am outputting, and where I put the write statement, and I can't figure it out.

Anybody know what I'm missing here? I've never had issues writing to file before...
 
Technology news on Phys.org
  • #2
Are you using an open() statement to open the file and "attach" it to unit 2? If so, what does it look like?
 
  • #3
A few questions/suggestions

How are you launching the executable?
double-click?
from command line?

If you double-click on it, the OS may be launching a separate terminal/dos window with its own environment and default working directory, etc...so, it may be running your program in some other directory and not the one you think.

To correct this situation, open a terminal and change directory to the directory where you actually have the executable. THEN, run the program by typing its name at the prompt.

If you are in Linux, make sure to list ALL files in the directory, even the ones typically hidden (.*)

To be sure, clean up the directory as much as possible to have only the files that you need to run the program...the executable itself and possibly an input file.

Run the program.

Inspect the directory one more time and see if an additional file appeared. If you did not explicitly open unit 2 and gave it a name...fortran will create a default file name along the lines of fort2 or something like that.
 
  • #4
The code creates the file just fine. I can change the name of the file if I want to. It just won't write anything to it. I get no compile or run errors either, just nothing being written to file.

open(unit=2,file="test.OUT")

I run from terminal.
 
  • #5
Is your file writable? (check directory permissions)

Also, I'd add a status='unknown' to the open command to make sure it's not worried about overwriting.

Tried printing the values of ist1,ist2 to screen with a print command at the same time?

Tried watching the file real-time to make sure it doesn't get populated then wiped? (tail -f test.OUT)

Tried changing the unit number to something (other than 5 or 6 of course)? (changing the open command also)

Tried adding a meaningless print *,"this does nothing" just above the write(2,*)? I know, this sounds like a weird suggestion, but if you overflow somewhere just before this it can screw or fix things automagically - the output might be buffered.

HTH.
 
  • #6
Yeah, that buffer thing is something good to keep in mind; although I doubt that's the problem right now, since it is said that the program run without errors.

But, when there are errors and the program terminates prematurely and does not write anything out, it IS because the write commands did not make it out of buffer. On those circumstances, I add a "flush" command after every set of "write" commands to force emptying the buffer and learn what is going on.

And, also...yes...the OPEN command could use more arguments; you may want to add the 'status' and say 'unknown' or 'new'; 'unknown' is the default, so, that should have work...but better to be safe.

Is your code very long? Can you post it? Or, before you do that...go ahead and do the same exercise of compiling a program, and running it the same way you are doing now...but instead, use this program:

Code:
program try
write(2,*) 'testing, testing, 1, 2, 3...'
end program try

This program works for me and it creates file "fort.2"

See what it does for you...if it works, you have a totally different problem...maybe you program is not quite running error free, maybe there is a memory mangling problem...I don't know...post it, if it is not too long.
 
  • #7
So it turns out that this does in fact write to file, but only after the entire program is finished completing. How can I ask it to perform these writes individually?
 
  • #8
my previous post, second paragraph
 
  • #9
gsal said:
my previous post, second paragraph

Flush is working. Awesome command, thank you!
 

1. Why is it important to write to the screen instead of the file in Fortran?

Writing to the screen allows for immediate output and feedback during program execution, which can be helpful for debugging and monitoring the progress of the program.

2. Can I still write to a file in Fortran if needed?

Yes, you can still write to a file in Fortran using the appropriate commands and file handling techniques. However, it is recommended to write to the screen for real-time feedback during program execution.

3. How do I write to the screen in Fortran?

To write to the screen in Fortran, you can use the WRITE(*,*) command, where the asterisk represents the screen as the output destination. This command can be used to display text, variables, and other data to the screen.

4. What is the difference between writing to the screen and writing to a file in Fortran?

The main difference is the output destination. Writing to the screen displays the output on the terminal or command line, while writing to a file stores the output in a designated file. Additionally, writing to the screen allows for immediate feedback, while writing to a file may require additional steps to view the output.

5. Can I customize the output format when writing to the screen in Fortran?

Yes, you can use formatting options in the WRITE(*,*) command to customize the output format. This includes specifying the number of digits, decimal places, and alignment for numerical values, as well as adding headers and other text for better readability.

Similar threads

  • Programming and Computer Science
Replies
2
Views
353
  • Programming and Computer Science
Replies
12
Views
7K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
17
Views
4K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
4
Views
589
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
18
Views
1K
Back
Top