-1

I want get new file in Fortran with extension (.for), for example: p12.12332.txt

I make:

CHARACTER filename*20
REAL tmp = 12.12332
filename = 'p00000000.txt'
write(filename(2:9),'(F12.5)') tmp

but I can not get file, I get

Fortran runtime error: End of record

What's problem? How can I file "p12.12332.txt" via Fortran?

  • 1
    What do you mean that you "get nothing"? Note, though, that you are trying to write a field of width 12 to a character substring of length 10. – francescalus Oct 20 '15 at 21:50
  • Perhaps you are looking for how to write to an internal record? – Ross Oct 20 '15 at 21:56
  • 1
    To a substring length 8 actually. You should use F8.5. You should better descibe your result. What is "nothing"? Is there any error message or something? (I expect your filename to contain `p********.txt`) – Vladimir F Героям слава Oct 20 '15 at 22:00
  • 2
    Other than that it is the same as http://stackoverflow.com/questions/1262695/converting-integers-to-strings-in-fortran http://stackoverflow.com/questions/13250123/reading-multiple-files-in-fortran?lq=1 and others. Just use the right format. – Vladimir F Героям слава Oct 20 '15 at 22:03
  • I didn't mean we should actually close it as a duplicate. It is strange to close it as a duplicate of converting integers, when there are no integers in this questions. – Vladimir F Героям слава Oct 21 '15 at 09:55
  • I agree with @vladimirf: my close vote was as a typo as the main problem of the question is solved by changing `12` to `8`. As the answer given says, there are other necessary things for usability, but that possibly requires the question to be in a different form – francescalus Oct 27 '15 at 08:35

1 Answers1

1

I get nothing,

you say. What I get are runtime errors.

  • Fortran runtime error: End of record for gfortran
  • forrtl: severe (66): output statement overflows record for ifort

That's because you're trying to write 12 characters into an 8 character buffer.

The first thing you can do is to correct the length:

write(filename(2:9), '(F8.5)') tmp

But this will cause unexpected behaviour if tmp < 10: It will have a space between the p and the number, like so: p 2.12332.txt

I think it would be better if you constructed the whole file name anew every time:

write(filename, '("p", F0.5, ".txt")') tmp
write(filename, '(A1, F0.5, A4)') "p", tmp, ".txt"

(Either one of these will work.)

This will change the length of the file name:

p12.23321.txt
p2.23321.txt

Otherwise you'll have to try with leading zeros, which is described here: How to pad FORTRAN floating point output with leading zeros?

Community
  • 1
  • 1
chw21
  • 7,970
  • 1
  • 16
  • 31