Mark S
Mark S

Reputation: 306

Fortran: how to remove file extension from character

I have a Fortran code that asks the user for an input file name. The code processes that file and I would like to write out another file based on the original file name. I think if I can understand how to remove the file extension then I can just concatenate whatever I want to the end of that.

How do I strip off the file extension in a character?

character(len=256):: input_file, output_file

Original file: AnyName.xxx

Output file: AnyName_Output.csv

Upvotes: 3

Views: 2234

Answers (1)

credondo
credondo

Reputation: 744

You can use scan to locate the position of the last dot in the string input_file. Then you can use that position to extract the input_file with no extension and concatenate the new one.

character(len=256):: input_file, output_file
integer :: ppos
character(len=3)  :: new_ext="csv"

ppos = scan(trim(input_file),".", BACK= .true.)
if ( ppos > 0 ) output_file = input_file(1:ppos)//new_ext

Upvotes: 8

Related Questions