Trouble with format in fortran

In summary, the conversation involved a user trying to use FORTRAN to write a .pvd file and seeking help with ensuring that the output is written on one line without spaces. The solution involved adjusting the format statement and removing the 1 in front of the real output.
  • #1
cjm2176
8
0
Hi all

I am trying to use fortran to write a .pvd file, an example of what one line of such a file should look like is

<DataSet timestep="1.00000E-07" part="0" file="Psb000001.vtu"/>

however with the following code:

Code:
write(90,2000) ttim,fname
2000  format('<DataSet timestep="',1pe15.5,'" part="0" file="',
     &       a,'"/>')

gives me the following result:

<DataSet timestep=" 1.00000E-07" part="0" file="Psb000001.vtu
"/>

which happens to result in a bad .pvd file. Is there a way to garuantee that everything gets written on one line? Also, is it possible to get rid of the spaces before 1.00000E-07?

Thanks!
cjm2176
 
Technology news on Phys.org
  • #2
Which FORTRAN are you writing in? I'm guessing 95?
 
  • #3
yes I'm using FORTRAN 95, the compiler is gfortran
 
  • #4
Let me play with this. I'll get back to you shortly.
 
  • #5
Just played with testing this out. Here is what I wrote.

Code:
      program testformat
      implicit none
	  
!23456789012345678901234567890123456789012345678901234567890123456789012	  

      real ttim
      character fname*13
  
      open(unit=10,file='test.txt')

      ttim = 1.0E-7 
      fname = "Psb000001.vtu"

      write(10,2000) ttim,fname
 2000 format('<DataSet timestep="',es13.7,'" part="0" file="',a,'"/>')

      close(10)
 
      return
      end program

FORTRAN can go out to 72 columns, so you don't need to truncate your format statement and continue to the next line. The whole thing can fit on one line. Taking the 1 out of in front of your real ouput will remove that space.
 
  • #6
works perfectly thanks!
 

1. What is the "format" keyword used for in Fortran?

The "format" keyword is used to specify the way data is read from or written to a file in Fortran. It determines the layout and type of data that is expected in a particular record of the file.

2. Why am I getting a "format error" when running my Fortran program?

A "format error" in Fortran typically means that the format specified in the program does not match the format of the data being read from or written to a file. This could be caused by incorrect format specifiers or mismatched data types.

3. How do I specify a fixed format in Fortran?

To specify a fixed format in Fortran, the "format" statement must be followed by a label or a star (*), and then a series of format specifiers that correspond to the data being read or written. For example, "format (I5, F10.2)" specifies a 5-digit integer followed by a 10-character floating-point number with 2 decimal places.

4. Can I use a variable as the format specifier in Fortran?

No, format specifiers in Fortran must be constant values and cannot be variables. However, you can use the "format" statement to dynamically generate format strings based on the value of a variable.

5. Is there a limit to the number of format specifiers that can be used in a single "format" statement in Fortran?

Yes, there is a limit to the number of format specifiers that can be used in a single "format" statement in Fortran. This limit is implementation-dependent and can range from a few hundred to several thousand.

Similar threads

  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
602
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
876
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
7
Views
6K
Back
Top