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

  • Fortran
  • Thread starter Saladsamurai
  • Start date
  • Tags
    Fortran
In summary, The person is trying to write to a text file without any leading whitespace on each line. They have tried using the Write(*,'(a)') statement and it works fine, but the value written is not always 2.00000000000. They would like to use a Double Precision PHI variable instead, but this produces nonsense in the file. They are trying to convert a Real type variable to a string in order to solve this issue.
  • #1
Saladsamurai
3,020
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
  • #2
If I could find a way to convert a type Real to a string I could do this. Anyone know how to do that?
 
  • #3
Something like this?
Code:
WRITE(17, FMT= '('REAC C2H4F2', 1X, F13.10)') PHI
 

1. What is the purpose of the FORTRAN write statement?

The FORTRAN write statement is used to output data to a file or to the screen. It allows the user to specify the format of the output and can be used to print both numerical and character data.

2. How is the format of the output specified in the write statement?

The format of the output is specified using format descriptors, which are placed within quotation marks after the data to be output. These descriptors indicate the type of data to be printed, such as integers, floating point numbers, or characters, and how they should be formatted, such as the width and precision of the data.

3. Can the write statement be used to append data to an existing file?

Yes, the write statement can be used to append data to an existing file by using the "append" keyword in the open statement. This allows data to be added to the end of the file without overwriting any existing data.

4. Are there any limitations on the number of variables that can be output using the write statement?

No, there are no limitations on the number of variables that can be output using the write statement. The only limitation is the number of characters that can be printed on a single line, which is typically 80 characters in most FORTRAN compilers.

5. Is the write statement platform-dependent?

No, the write statement is not platform-dependent. It is a standard feature of FORTRAN and is supported by all modern FORTRAN compilers. However, the format descriptors used in the write statement may vary slightly between different compilers.

Similar threads

  • Programming and Computer Science
Replies
4
Views
601
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
19
Views
5K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top