Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to name output files (character appended to an integer)

Status
Not open for further replies.

sleep

IS-IT--Management
Jun 18, 2002
5
US
I have a file that contains large amounts of data. I need to split it into many files that contain a set amount of enteries. My problem is that i have no clue how to make fortran name the output files automatically.
Examplary file names:
image1
image2
image3
..
image[n]
How do you append a number to a character string?
 
Try something like this for up to 100 files (image001.out to image100.out) You are actually writing the integer value into the string position. Good luck

CHARACTER FNAME3*12

...
...
INAM = 5
FNAME3(1:INAM) = "IMAGE"


C Run loop to create files

DO 30 IRUN = 1, NRUN

C Assemble input file name

IF (IRUN .LE. 9) THEN

FNAME3(INAM+1:INAM+2) = '00'
FNAME3(INAM+4:INAM+7) = '.out'
WRITE(FNAME3(INAM+3:INAM+3), '(I1)') IRUN

ELSEIF (IRUN .LE. 99) THEN

FNAME3(INAM+1:INAM+1) = '0'
FNAME3(INAM+4:INAM+7) = '.out'
WRITE(FNAME3(INAM+2:INAM+3), '(I2)') IRUN

ELSE

FNAME3(INAM+4:INAM+7) = '.out'
WRITE(FNAME3(INAM+1:INAM+3), '(I3)') IRUN

ENDIF

C Open file and write data

OPEN(UNIT=4, FILE=FNAME3, ACCESS='SEQUENTIAL',
1 FORM='FORMATTED', STATUS='NEW')

WRITE(4,.....

...
...

CLOSE (UNIT=4)

30 CONTINUE
 
A somewhat simpler way build the file name based on the code in the previous reply is to use the "Iw.m" format code. This code will add leading zeros to the number.
--- snip ---
C Set the suffix one time
FNAME3(INAM+4:INAM+7) = '.out'
C Run loop to create files
DO 30 IRUN = 1, NRUN
C Assemble input file name
IF (IRUN .LE. 999) THEN
WRITE(FNAME3(INAM+1:INAM+3), '(I3.3)') IRUN
ELSE
error recovery code should go here if more than 999 files
ENDIF
C Open file and write data
OPEN(UNIT=4, FILE=FNAME3, ACCESS='SEQUENTIAL',
1 FORM='FORMATTED', STATUS='NEW')
WRITE(4,.....
--- snip ---

This will create a series of files named
IMAGE001.out
IMAGE002.out
.
.
IMAGE999.out
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top