Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.

Append Text in a File?

ratzeputz
Beginner
2,523 Views
Hi there,
how do i append a text in a text file, which already includes text?
Like this:
Text in File before opening within Fortran: "Hello"
Opening File in Fortran
Write Text to File within Fortran: "World"
Close File in Fortran
Text in File after closing within Fortran: "Hello World"
Hope you can help me, cause opening and writing a text (regardless binary or text mode) is not the problem....just the text appending.
Regards
Oliver
0 Kudos
3 Replies
Arjen_Markus
Honored Contributor II
2,523 Views
You can specify position="append".
Then the file pointer will be positioned to the end of the file.

However, if you want to append to the previous _line_, that requires preparation:
you need to write that last line via advance="no", so that the newline which is
normally added is suppressed.

As far as I know, you can not go back one character in the file, unless you use
stream access on a text file, but then the problem is: is the newline a single
character or is it carriage-return - linefeed?

Regards,

Arjen
0 Kudos
anthonyrichards
New Contributor III
2,523 Views
Suppose you want to write "World, Hello" when you already have "Hello".
You will have to replace the contents of the record with the string "World, Hello".
It's the same with wanting to replace "Hello" with "Hello World".
So you have to rewind the file, move the file marker to the beginning of the record
that you are interested in and then write your new contents to it.
If you have a string variable containing "Hello", and a string variable containing " World" then you can append another string to it in your code by using
helloworldstring=trim(hellostring)//trim(worldstring)
provided that helloworldstring has been sized long enough. You can then WRITE the whole string to the record using A format.
Otherwise you just output the two strings back to back to the record using a WRITE and A format.

0 Kudos
ratzeputz
Beginner
2,523 Views
Thanks to both of you for your answers.

@arjenmarkus:
Well...i did not expect, that the solution is quite that easy.
Because i alwaysonly need to append text at the end of the file, theposition="append" flag is enough for me.
Many thanks :)
0 Kudos
Reply