Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

writing output listing onto screen (or not) and onto file
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

Report this thread to moderator Post Follow-up to this message
Old Post
Bernard Bru
07-17-06 08:59 AM


Re: writing output listing onto screen (or not) and onto file
Bernard 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?

Report this thread to moderator Post Follow-up to this message
Old Post
Tim Prince
07-17-06 12:59 PM


Re: writing output listing onto screen (or not) and onto file
Tim 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

Report this thread to moderator Post Follow-up to this message
Old Post
bru
07-17-06 12:59 PM


Re: writing output listing onto screen (or not) and onto file
bru 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


Report this thread to moderator Post Follow-up to this message
Old Post
jwm
07-18-06 12:00 AM


Re: writing output listing onto screen (or not) and onto file
jwm 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

Report this thread to moderator Post Follow-up to this message
Old Post
bru
07-18-06 12:00 AM


Re: writing output listing onto screen (or not) and onto file
bru 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 /)


Report this thread to moderator Post Follow-up to this message
Old Post
jwm
07-18-06 12:00 AM


Re: writing output listing onto screen (or not) and onto file
Tim 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

Report this thread to moderator Post Follow-up to this message
Old Post
bru
07-18-06 12:00 AM


Re: writing output listing onto screen (or not) and onto file
On 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


Report this thread to moderator Post Follow-up to this message
Old Post
Richard Maine
07-18-06 12:00 AM


Re: writing output listing onto screen (or not) and onto file
jwm 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

Report this thread to moderator Post Follow-up to this message
Old Post
bru
07-18-06 12:00 AM


Re: writing output listing onto screen (or not) and onto file
On 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

Report this thread to moderator Post Follow-up to this message
Old Post
Ken Fairfield
07-18-06 12:00 AM


Sponsored Links




Last Thread Next Thread Next
Pages (2): [1] 2 »
Search this forum -> 
Post New Thread

Fortran archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 03:27 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.