Code Comments
Programming Forum and web based access to our favorite programming groups.Is it possible to simplify my code by putting only one
WRITE statement when I want to write output results onto screen
(or not) and onto a file :
I put :
CHARACTER*1 SEL
OPEN( 8,FILE='sosout')
WRITE(6,*) ' Sorties en ligne (o | n ou > )?'
READ (5,'(A)') SEL
IF(SEL.NE.'o') OPEN(6,FILE='NO')
WRITE(6,200) A, B, C
WRITE(8,200) A, B, C
200 FORMAT(' A B C=', 3F12.4)
...........;
Thanks for your help!
Bernard Bru
Post Follow-up to this messageBernard Bru wrote: > Is it possible to simplify my code by putting only one > WRITE statement when I want to write output results onto screen > (or not) and onto a file : > Are you asking about facilities provided by most systems for redirecting output sent to '*' (or, on many systems, unit 6 without an OPEN)? For example, the tee program, or redirection using '>' on the command line?
Post Follow-up to this messageTim Prince wrote: > Bernard Bru wrote: > > Are you asking about facilities provided by most systems for redirecting > output sent to '*' (or, on many systems, unit 6 without an OPEN)? > For example, the tee program, or redirection using '>' on the command line?[/color ] Sorry, perhaps I'm not so clear: Some time I want to have at the same time output on screen (Unit 6 or *) and onto a file (OPEN(8,FILE= 'sosout') An an other time I want only output on file (here unit 8) (for me it is easier to visualize the output using vim editor) So I put in this case OPEN(6,FILE= 'NO') because I dont need it It seems to me heavy to put in the code 2 WRITE's for each output!!! WRITE(6,.................... WRITE(8, the same as above Bernard Bru
Post Follow-up to this messagebru wrote:
> Some time I want to have at the same time output on screen (Unit 6 or *)
> and onto a file (OPEN(8,FILE= 'sosout')
> An an other time I want only output on file (here unit 8) (for me it is
> easier to visualize the output using vim editor) So I put in this case
> OPEN(6,FILE= 'NO') because I dont need it
What about this:
integer, parameter :: USR_UNIT(2) = [6, 8]
integer i
logical StdOutput, FileOutput
real a, b, c
a = 1.; b = 2.; c = 3.
write(*, '("Std output? [T/F]: ")', ADVANCE = 'NO')
read(*, *) StdOutput
write(*, '("File? [T/F]: ")', ADVANCE = 'NO')
read(*, *) FileOutput
if (FileOutput) Open(unit = USR_UNIT(2), file = 'userfile.txt')
do i = 1, 2
if (i == 1 .AND. (.NOT.StdOutput)) cycle
if (i == 2 .AND. (.NOT.FileOutput)) cycle
write(USR_UNIT(i), "(' A B C=', 3F12.4)") a, b, c
enddo
Post Follow-up to this messagejwm wrote:
> bru wrote:
>
>
>
>
>
> What about this:
>
> integer, parameter :: USR_UNIT(2) = [6, 8]
> integer i
> logical StdOutput, FileOutput
> real a, b, c
>
> a = 1.; b = 2.; c = 3.
>
> write(*, '("Std output? [T/F]: ")', ADVANCE = 'NO')
> read(*, *) StdOutput
>
> write(*, '("File? [T/F]: ")', ADVANCE = 'NO')
> read(*, *) FileOutput
>
> if (FileOutput) Open(unit = USR_UNIT(2), file = 'userfile.txt')
>
> do i = 1, 2
> if (i == 1 .AND. (.NOT.StdOutput)) cycle
> if (i == 2 .AND. (.NOT.FileOutput)) cycle
> write(USR_UNIT(i), "(' A B C=', 3F12.4)") a, b, c
> enddo
>
I tried to compile your code: it works with g95, ifort and pgf90 but not
with f90 of Sun; error messages are:
integer, parameter :: USR_UNIT(2) = [6, 8]
^
"io.f90", Line = 2, Column = 40: ERROR: Unexpected syntax: "operand" was
expected but found "[".
^
"io.f90", Line = 2, Column = 44: ERROR: Unexpected syntax: "object-name"
was expected but found "8".
f90: COMPILE TIME 0.090000 SECONDS
f90: MAXIMUM FIELD LENGTH 4282502 DECIMAL WORDS
f90: 22 SOURCE LINES
f90: 2 ERRORS, 0 WARNINGS, 0 OTHER MESSAGES, 0 ANSI
nor with f95 of NAG :
Error: io.f90, line 2: Illegal character '['
Error: io.f90, line 2: syntax error
***Invalid item in type declaration
Error: io.f90, line 2: Illegal character ']'
[f95 terminated - errors found by pass 1]
I'm not a specialist of Fortran 90!!
Your solution seems to me to heavy because I have hundred of writes in
my code!!
Thanks a lot for your ansver
Bernard Bru
Post Follow-up to this messagebru wrote: > > I tried to compile your code: it works with g95, ifort and pgf90 but not > with f90 of Sun; error messages are: > > > integer, parameter :: USR_UNIT(2) = [6, 8] > ^ > "io.f90", Line = 2, Column = 40: ERROR: Unexpected syntax: "operand" was > expected but found "[". > ^ > "io.f90", Line = 2, Column = 44: ERROR: Unexpected syntax: "object-name" > was expected but found "8". Yes, It's a f2003 feature, sorry about that. Try using: integer, parameter :: USR_UNIT(2) = (/ 6, 8 /)
Post Follow-up to this messageTim Prince wrote: > Bernard Bru wrote: > > Are you asking about facilities provided by most systems for redirecting > output sent to '*' (or, on many systems, unit 6 without an OPEN)? > For example, the tee program, or redirection using '>' on the command line?[/color ] Your solution is good for me in the case I want to have results only on a file. In the other case I want to have results at the same time on the screen and on a file!! Thanks for your ansver Bernard Bru
Post Follow-up to this messageOn Mon, 17 Jul 2006 08:15:09 -0700, bru wrote (in article <e9g9ht$662$1@ccpntc8.in2p3.fr> ): > Some time I want to have at the same time output on screen (Unit 6 or *) > and onto a file (OPEN(8,FILE= 'sosout') ... > ^ > "io.f90", Line = 2, Column = 40: ERROR: Unexpected syntax: "operand" was > expected but found "[". That's just because of the use of the [] for array constructors. That is an f2003 feature adopted as an extension by some f95 compilers, but you can't portably count on it in f95. But that is peripheral to the question. > Your solution seems to me to heavy because I have hundred of writes in > my code!! Yes. I'd agree that this is a bit much for most applications. It requires too much structuring of the code around this particular I/O requirement. If you want output to go to *EITHER* the terminal or a file, that is relatively simple. There is a minor system-dependent piece needed to get a unit number that is connected to standard output (f2003 finally has a standard way to do this). Then you can select between that unit or a unit connected to some other file. I do that kind of thing regularly. But if you want the output to go to both places, there isn't a "nice" way to do that within Fortran. It requires that two WRITE statements be executed. Something like the trick that jwm illustrated can be used to make one statement that is executed twice, but still, the fundamental issue is that there have to be two writes, whether written as two separate statements, or as a single statement executed twice. There are often ways outside of Fortran to do this. See the Unix "tee" command (although sometimes it has "issues" with buffering, which can cause awkwardness for interactive use.) -- 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
Post Follow-up to this messagejwm wrote:
> bru wrote:
>
>
>
>
>
> What about this:
>
> integer, parameter :: USR_UNIT(2) = [6, 8]
> integer i
> logical StdOutput, FileOutput
> real a, b, c
>
> a = 1.; b = 2.; c = 3.
>
> write(*, '("Std output? [T/F]: ")', ADVANCE = 'NO')
> read(*, *) StdOutput
>
> write(*, '("File? [T/F]: ")', ADVANCE = 'NO')
> read(*, *) FileOutput
>
> if (FileOutput) Open(unit = USR_UNIT(2), file = 'userfile.txt')
>
> do i = 1, 2
> if (i == 1 .AND. (.NOT.StdOutput)) cycle
> if (i == 2 .AND. (.NOT.FileOutput)) cycle
> write(USR_UNIT(i), "(' A B C=', 3F12.4)") a, b, c
> enddo
>
In my case your code is a little bit simpler with a test only on
StdOutput :
integer, parameter :: USR_UNIT(2) = (/6, 8/)
integer i
logical StdOutput
real a, b, c
a = 1.; b = 2.; c = 3.
write(*, '("Std output? [T/F]: ")', ADVANCE = 'NO')
read(*, *) StdOutput
Open(unit = USR_UNIT(2), file = 'userfile.txt')
do i = 1, 2
if (i == 1 .AND. (.NOT.StdOutput)) cycle
write(USR_UNIT(i), "(' A B C=', 3F12.4)") a, b, c
enddo
end
Post Follow-up to this messageOn 7/17/2006 8:15 AM, bru wrote: [...snip example of selecting output units...] > Your solution seems to me to heavy because I have hundred of writes in > my code!! In priniciple, you could generalize jwm's example to any number of units/files, but isolate the multiple WRITE's to a subroutine you've written, e.g., MY_WRITE, and then replace all the WRITE's with MY_WRITE's in your code. Unfortunately in practice, that may be quite difficult because of needing to pass a variable number of arguments to MY_WRITE. Only if the number of different combinations of format, argument number and argument types is fairly small would this be feasible (presumably be using a generic feature to overload the finite number of different versions...). -Ken -- I don't speak for Intel, Intel doesn't speak for me... Ken Fairfield D1C Automation VMS System Support who: kenneth dot h dot fairfield where: intel dot com
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.