Fortran: Write statement Position specifier for Terminal - fortran

This program will write 'hello' at the specified position '9' of the file 'test.txt'
program test
open(31,file='test.txt',access='stream')
write(31,pos=9)'hello'
close(31)
end program
Can this position specifier be used to write on terminal?
I want to be able to do something like write(*,pos=9)'hello'

The standard output is pre-opened as a sequential access formatted file, therefore you cannot use pos.
Why you want this? You can just use an explicit format
write(*,'(t9,a)') 'hello'
Or you can print some spaces before hello. You can even concatenate the spaces and the string.

Related

What is the ^# symbol at the end of my output txt file?

I wrote c++ code to read from a text file and then bash code to output it to another file (specifically './executable &> output.txt'). When I print it on the command line it looks fine, but when I check the output file, it has a '^#' symbol at the end of it.
You have a bug in your program, it's writing a nul character at the end of your file. Then whatever tool you use to check the output is using: caret notation for non-printable characters.
^# is the notation for the nul character.
We cannot tell more without seeing the code.

Is '(A)' a keyword?

I have a doubt about Fortran code. Is "A" a keyword? I found this character used in write and format commands but I can't find some specific documentation about it. Here are some examples:
CHARACTER *10 name
write(*,1) name
1 format (" Your name is ",A)
or
end = LNBLNK(string)
write(4,'(A)') string(1:end)
No, A is an data edit descriptor for character string output. For more read some textbook about Fortran I/O or a tutorial like https://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/format.html
And it the second code sample, A is not even in a position where a keyword would be used. '(A)' is a normal string, like 'Hello world' or " Your name is ", for example. But here the content of the string and where the string is used is what matters. It contains a descriptor and it is used as a format string in the write statement.
In the FORMAT statement it is not a normal string, but a special syntax, but it works the same.

Can Fortran format statement be written as character string before use?

I have a Fortran program in which I use a format staement for writing like
WRITE (12,'(A72,1X,A,1X,I6.6)') ALINE,SFFIX(1:NUM),LINENO
which works fine. However when I write a character string `fmtt' like
WRITE (fmtt,'(a)') trim(adjustl(sttr(2)))
where string 'sttr (2)is '(A72,1X,A,1X,I6.6) ' ` , which I confirm by printing fmtt like
WRITE(*,'(a)')fmtt
When I use string fmtt as format in a write statement like
WRITE (12, fmtt) ALINE,SFFIX(1:NUM),LINENO
I get error message
forrtl : info(58) format syntax error at or near '(A72,1X,A,1X,I6.6) '
Though I am not an expert I expected it to work as format is supposed to be a character string. Where am I wrong? I wanted to do this to make format dependent on user input. Thanking you.
Probably the single quotation marks ' ' are making problem. Following code is working fine.
implicit none
character(len=30) :: fmtt
fmtt = '(A72,1X,A,1X,I6.6)'
write(*,fmtt)"first_character","second_character",230
stop
end
Note that print*,fmtt give the output without single quotes. I follow the method given in http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap05/format.html . Hopefully this will help you.

How to remove leading space when printing with write?

Suppose I have the following code
program fortran
open(900, FILE='SOMETHING')
write(900, *) '21'
end program fortran
The file form will be
21
that is, there is a space before the number. How to get rid of that space?
You can write it as a string:
PROGRAM fortran
OPEN(900,FILE='SOMETHING')
WRITE(900,'(a)') '21'
END PROGRAM FORTRAN
> cat SOMETHING
21
To respond to the comment:
The more explicit way of doing that would be to write the number into a string (you could also use list-directed I/O for this step), remove whitespaces from the string trim and finally output the left-adjusted adjustl:
program test
character(len=23) :: str
write(str,'(ES23.15 E3)') 1.23d0
write(*,'(a)') adjustl(trim(str))
write(str,'(ES14.7 E2)') 0.12e0
write(*,'(a)') adjustl(trim(str))
end program
> ./a.out
1.230000000000000E+000
1.2000000E-01
This solution is probably more complicated then necessary, but it is a very flexible approach that can be extended easily for arbitrary purposes and formats.
In list-directed format (the * in write(unit,*)) the compiler typically inserts a leading space. The first column used to be used to control line printers but that is now deleted from Fortran.
You can use any explicit format you want to get rid of the leading space. For example the general g0 one or the string specific a or the integer-specific i.

Writing on the same line in FORTRAN

In Fortran, each time one uses WRITE a new line is produced. In order to control the working of a program that is being executed, I would like to write on screen the current value of a variable, but always on the same line (erasing the previous value and starting at the beginning of the line). That is, something like
1 CONTINUE
"update the value of a"
WRITE(*,*) a
BACKSPACE "screen"
GOTO 1
Something like WRITE(*,*,ADVANCE='NO') (incorrect anyway) is not quite what I
need: this would write all the values of a one after another on a very long
line.
A trick that I was shown for what you want is as follows
do l=1,lmax
...update a...
write(*,'(1a1,<type>,$)') char(13), a
enddo
where <type> is your format specifier for a (i.e., i0 for integer).
The key is the char(13), which is the carriage return, and the $ in the format descriptor. I really don't know if there is a name for $, I just know that it works for displaying on the screen--for output to file you get an a on each line.