Fortran How to Avoid Leading Whitespace When Writing to a Text File in FORTRAN?

  • Thread starter Thread starter Saladsamurai
  • Start date Start date
  • Tags Tags
    Fortran
AI Thread Summary
The discussion centers on writing to a text file in Fortran without leading whitespace on each line. The initial approach using the Write(*,'(a)') statement works for fixed strings but fails when trying to include a variable (PHI) in the output. The user seeks a method to convert a double precision variable to a string format without using statement labels. A proposed solution involves using formatted output with the WRITE statement, specifically suggesting the syntax WRITE(17, FMT= '('REAC C2H4F2', 1X, F13.10)') PHI to achieve the desired formatting. The key challenge remains finding a way to properly format the variable for output without introducing unwanted whitespace.
Saladsamurai
Messages
3,009
Reaction score
7
I am writing to a text file and a do not want any leading whitespace on each line. I have been trying the Write(*,'(a)') statement. This works just fine:

Code:
Program MyTest

Open(2,File = 'testFile.inp')

Write(2,'(a)')'DIAG'
Write(2,'(a)')'REAC C2H4F2 2.00000000000'

Close(2)
End

However, the value written is not always 2.00000000000 and hence I would like something like:
Code:
Program MyTest
Double Precision PHI

PHI = 2.0

Open(2,File = 'testFile2.inp')

Write(2,'(a)')'DIAG'
Write(2,'(a)')'REAC C2H4F2 ', [B]PHI[/B]

Close(2)
End
I am trying to avoid statement labels if possible.

However the latter produces nonsense in the file for PHI.
 
Technology news on Phys.org
If I could find a way to convert a type Real to a string I could do this. Anyone know how to do that?
 
Something like this?
Code:
WRITE(17, FMT= '('REAC C2H4F2', 1X, F13.10)') PHI
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
8
Views
2K
Replies
5
Views
5K
Replies
2
Views
2K
Replies
3
Views
3K
Replies
6
Views
3K
Replies
19
Views
6K
Replies
2
Views
1K
Back
Top