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

  • Context: Fortran 
  • Thread starter Thread starter Saladsamurai
  • Start date Start date
  • Tags Tags
    Fortran
Click For Summary
SUMMARY

This discussion focuses on writing to text files in FORTRAN without leading whitespace. The user successfully uses the Write(*,'(a)') statement to write fixed strings but encounters issues when trying to write a variable, specifically a Double Precision value, without leading spaces. The proposed solution involves using formatted output with the WRITE statement, specifically the syntax WRITE(17, FMT= '('REAC C2H4F2', 1X, F13.10)') PHI to achieve the desired output format.

PREREQUISITES
  • Understanding of FORTRAN programming language
  • Familiarity with FORTRAN I/O operations
  • Knowledge of data types in FORTRAN, specifically Double Precision
  • Experience with formatted output in FORTRAN
NEXT STEPS
  • Research FORTRAN formatted output techniques
  • Learn about data type conversions in FORTRAN
  • Explore advanced I/O operations in FORTRAN
  • Investigate the use of WRITE statements for dynamic data in FORTRAN
USEFUL FOR

FORTRAN developers, programmers working on scientific computing, and anyone needing to manage text file output without leading whitespace in their applications.

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
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 19 ·
Replies
19
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K