Home > Archive > Fortran > June 2005 > writing \ character
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
writing \ character
|
|
| Patrick Begou 2005-06-09, 3:58 pm |
| Hi,
I have some trouble while trying to write a character literal constant
in a file. This character literal constant contains a '\' character.
Small example:
PROGRAM bide
OPEN(10,file='bide.txt',FORM='FORMATTED')
WRITE(10,'(a)') "\une"
CLOSE(10)
END
This works with Intel compiler (file contains "\une") but with ibm xlf,
bide.txt only contains "une" without backslash.
If on IBM I use:
WRITE(10,'(a)') "\\une"
instead of
WRITE(10,'(a)') "\une"
bide.txt contains "\une". But back on the intel node it contains now '\\une'
Wich compiler is wrong and how can I write a portable code ?
Thanks for your help.
Patrick
| |
|
| the following might work on ASCII machines
write(6,*) char(92)//"une"
Joost
| |
| Richard E Maine 2005-06-09, 3:58 pm |
| In article <d89irt$14n$1@news.grenet.fr>,
Patrick Begou <Patrick.Begou@hmg.inpg.fr> wrote:
> I have some trouble while trying to write a character literal constant
> in a file. This character literal constant contains a '' character.
....
> Wich compiler is wrong and how can I write a portable code ?
Well, the backslash is not in the Fortran character set for f95 (though
it is in f2003), so you can't use it in an f95 program and claim to be
100% standard-conforming and portable.
Some compilers do C-style substitutions, which "mess up" attempts to use
backslashes. Personally, I feel that this is a strange and annoying
thing for a Fortran compiler to do, but I can't quite argue that a
compiler is definitively "wrong" to do it (though I'll have a much
stronger argument in f2003).
Note that some compilers that do the substitution have an option to turn
it off.
Other than beating on vendors to "fix" what I consider to be brokenness,
or using switches to turn off the "feature", there are 2 reasonably
portable methods of working around the pain. Both involve defining a
named constant, which you then use elsewhere instead of the literal. If
you really want to nicely put the backslash in a literal, you have use
the compiler switches (or beat on the vendor).
The most "straightforward" (IMO) method is to use the ACHAR intrinsic.
Looking up in an ASCII table, I see that backslash is ASCII character
92, so do
character*1, parameter :: back = achar(92) !-- ASCII backslash.
The other method doesn't require the "magic number", but is a bit
tricky. Someone else here suggested it; I apologize for forgetting who;
it is sort of inventive.
character*1, parameter :: back = '\' !-- Single backslash.
If your compiler does C-style backslash processing then the \\ turns
into a single \, which is what you want. The tricky part here is that if
the compiler doesn't do the C thing, it works anyway. That's because the
literal constant then has 2 backslashes, but the second one gets
truncated to fit in the character*1.
--
Richard Maine | Good judgment comes from experience;
email: my first.last at org.domain | experience comes from bad judgment.
org: nasa, domain: gov | -- Mark Twain
| |
| John Harper 2005-06-10, 4:00 am |
| In article <nospam-4ED82C.08490509062005@news.supernews.com>,
Richard E Maine <nospam@see.signature> wrote:
>
>The other method doesn't require the "magic number", but is a bit
>tricky. Someone else here suggested it; I apologize for forgetting who;
>it is sort of inventive.
>
> character*1, parameter :: back = '\' !-- Single backslash.
I suggested it here, with back*1 instead of character*1 to avoid being
accused of obsolescence. I don't deserve the apology, as I got the idea
from whoever wrote the section on backslash in the g77 website. All I
did was tell c.l.f about it. By the way, that *1 is unnecessary for the
compiler but is useful to the human reader, because the default is *1.
John Harper, School of Mathematics, Statistics and Computer Science,
Victoria University, PO Box 600, Wellington, New Zealand
e-mail john.harper@vuw.ac.nz phone (+64)(4)463 5341 fax (+64)(4)463 5045
| |
| Patrick Begou 2005-06-10, 8:57 am |
| Thanks for all the answers. Following your ideas, I have changed my test
program in:
PROGRAM bide
CHARACTER(LEN=1), PARAMETER:: back='\\'
OPEN(10,file='bide.txt',FORM='FORMATTED')
WRITE(10,'(a)') back//"une"
CLOSE(10)
END
and now the bide.txt file contains "\une" whith xlf and with ifort
without any warning at compile time.
I will use this syntax in my application.
Patrick
|
|
|
|
|