Fortran 77 File I/O Write and Format Question 
Author Message
 Fortran 77 File I/O Write and Format Question

Does anyone know how to write to a file without going to the next
line?  I am trying to print out real and integer numbers not knowing
which is which.  Also I need to print them in columns, so I pretty
much want to print the first character no matter integer or real, then
tab over a couple of spaces and print out another number either
integer or real.  This is what I'm doing (spacing is correct unlike
the pseudo below):

do i=1,10
  if(integer)
    write(file1,200) integer(i)
  else
    write(file1,201) real(i)
  endif
enddo

    write(file1,*)               ! new line character

200   format('+',T5,i8)          
201   format('+',T5,f8.3)

It doesn't seem to stay on the same line.  When I reach a certain
point is when I want the new line character to be printed. So the
output file would look similar to this:

1          2.03         45            3.92        12         78.021  
 ....
34.21      6            18.091        2           9          6        
 ....
...
...
...

Thanks for help in advance
J



Tue, 15 Feb 2005 01:05:12 GMT  
 Fortran 77 File I/O Write and Format Question

Quote:

> Does anyone know how to write to a file without going to the next
> line?

A frequently-asked question.  In (very) short... there is no standard
way in f77.  The "+" stuff you are trying to use is for "printers"
and can't portably be counted on for your kind of application.
There is a standard way in f90 (advance="no"), and most f77 compilers
support one of several non-standard ways.  Probably the most common
involves a $ used as an edit descriptor, but there are other variants,
including I think, some f77 compilers that have adopted the f90 form.

In many cases, the desired result can be most easily achieved
by doing internal writes to character variables and then writing
the character variables out to the file when you have a line's worth
built.  That would seem to be appropriate for your application.

Quote:
> I am trying to print out real and integer numbers not knowing
> which is which.  Also I need to print them in columns,....

Check out the G format descriptor.  It works with both integers and
reals (and some other types as well) and may be just what you need.
That would be a very simple solution.  Otherwise, I suggest the
internal write approach as the next option.  You could make the
nonstandard f77 equivalents of the f90 advance="no" do the trick,
but I personally think that, since the the internal write solution
will work, I think it better than fussing with portability issues.

--
Richard Maine                       |  Good judgment comes from experience;
email: my last name at host.domain  |  experience comes from bad judgment.
host: altair, domain: dfrc.nasa.gov |        -- Mark Twain



Tue, 15 Feb 2005 01:57:43 GMT  
 Fortran 77 File I/O Write and Format Question
Thank you for the help but,
Quote:
> > Does anyone know how to write to a file without going to the next
> > line?

> A frequently-asked question.  In (very) short... there is no standard
> way in f77.  The "+" stuff you are trying to use is for "printers"
> and can't portably be counted on for your kind of application.
> There is a standard way in f90 (advance="no"), and most f77 compilers
> support one of several non-standard ways.  Probably the most common
> involves a $ used as an edit descriptor, but there are other variants,
> including I think, some f77 compilers that have adopted the f90 form.

If I were to do it this way how would the syntax look like:
     format(t5,i5,'$')
and this would not print the carriage return? So I can call it in the loop
and I would be able to print the carriage return doing this:
     write(file1,111)
111  format(' ')

Quote:
> In many cases, the desired result can be most easily achieved
> by doing internal writes to character variables and then writing
> the character variables out to the file when you have a line's worth
> built.  That would seem to be appropriate for your application.

I'm not sure as to how this would be done.  I'll figure out what my data
is and then stick it into a character variable array in my loop? Then I'd
print it out as such:
      write(file1,123) array(1),array(2),...
123   format(a5,a5,...)

Quote:
> > I am trying to print out real and integer numbers not knowing
> > which is which.  Also I need to print them in columns,....

> Check out the G format descriptor.  It works with both integers and
> reals (and some other types as well) and may be just what you need.
> That would be a very simple solution.  Otherwise, I suggest the
> internal write approach as the next option.  You could make the
> nonstandard f77 equivalents of the f90 advance="no" do the trick,
> but I personally think that, since the the internal write solution
> will work, I think it better than fussing with portability issues.

I've tried this but my data is still printed out in fixed point numbers so
the integer variables still have the trailing decimal and zeros.  Unless
I was just formatting it all wrong.

Thanks again,
J



Tue, 15 Feb 2005 15:34:07 GMT  
 Fortran 77 File I/O Write and Format Question

Quote:

> Thank you for the help but,
> > > Does anyone know how to write to a file without going to the next
> > > line?

> > A frequently-asked question.  In (very) short... there is no standard
> > way in f77.  The "+" stuff you are trying to use is for "printers"
> > and can't portably be counted on for your kind of application.
> > There is a standard way in f90 (advance="no"), and most f77 compilers
> > support one of several non-standard ways.  Probably the most common
> > involves a $ used as an edit descriptor, but there are other variants,
> > including I think, some f77 compilers that have adopted the f90 form.

> If I were to do it this way how would the syntax look like:
>      format(t5,i5,'$')
> and this would not print the carriage return? So I can call it in the loop
> and I would be able to print the carriage return doing this:
>      write(file1,111)
> 111  format(' ')

No, the correct format is:

   format(t5,i5,$)

$ is an extra formatting type, not a literal string.

Quote:

> > In many cases, the desired result can be most easily achieved
> > by doing internal writes to character variables and then writing
> > the character variables out to the file when you have a line's worth
> > built.  That would seem to be appropriate for your application.

> I'm not sure as to how this would be done.  I'll figure out what my data
> is and then stick it into a character variable array in my loop? Then I'd
> print it out as such:
>       write(file1,123) array(1),array(2),...
> 123   format(a5,a5,...)

No, something along these lines:

   character*200 longln

   write( longln, '(20F10.3)' ) ( array(i), i=1,20 )
   write( 10, * ) longln  

Quote:
> > > I am trying to print out real and integer numbers not knowing
> > > which is which.  Also I need to print them in columns,....

But the stuff is stored in a typed variable. I guess you mean
you do not know if the _value_ is integer or not?

   if ( real(int(value)) .eq. value ) then
      write(*,*) 'Integer! use (I10)'
   else
      write(*,*) 'Real! use (F10.0)'
   endif

might be what you want.

Regards,

Arjen



Tue, 15 Feb 2005 15:52:36 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. FORTRAN 77 blank lines in formatted write

2. fortran 77 i/o of poorly formatted file

3. File access question (Fortran 77) - please e-mail answer

4. $ sign in fortran 77 FORMAT statement ??

5. fortran 77 formatted output for floating-point

6. HELP:fortran 77 input formatting

7. Fortran 77 FORMAT statement

8. Writing CGI programs in Fortran 77

9. Editors written in FORTRAN 77...

10. Wanted: Access of Commandline in OS/9 Fortran 77

11. Fomatted read from a csv file (FORTRAN 77)

12. Wanted: MS Windows help file for Fortran 77

 

 
Powered by phpBB® Forum Software