user1515438
user1515438

Reputation: 23

How do I write to the end of the line (or tab to a variable)?

I'm writing a code in which I'd like to append a file name to the end of the lines. There are two logical ways that this can be done, but I don't know if those exist in Fortran.

  1. Simply append a character variable to the end of the line that I'm reading.

  2. [PREFERRED] use the T (tab) descriptor with a numerical variable that tells it which column to skip to, and then write the character variable starting at that column.

Upvotes: 2

Views: 1754

Answers (1)

bananafish
bananafish

Reputation: 2917

Read each line as a string, then trim whitespace at the end of the string and append filename at the end. Make sure that your character variable is large enough. Below is a simple program that illustrates the idea.

program append_to_rows

implicit none

integer :: j
character(len=10) :: fname = 'mydata.txt'
character(len=100) :: row

open(1,file=fname, status='old')
open(2,file='processed.txt', status='unknown')

do j=1,6
    read(1,'(a)') row
    row = trim(row)//fname
    write(2,'(a)') trim(row)
end do

close(1)
close(2)

end program

mydata.txt:

1,2,3,4,dfkldf
1,2,3fdfkj
1

123
  3434j,43,5

processed.txt:

1,2,3,4,dfkldfmydata.txt
1,2,3fdfkjmydata.txt
1mydata.txt
mydata.txt
123mydata.txt
  3434j,43,5mydata.txt

Upvotes: 3

Related Questions