Silverfrost Forums

Welcome to our forums

Reading a text file

15 Oct 2011 3:58 #9096

Hi,

I have to read a text file in Fortarn.The text file has around 200 lines.

The first 100 lines (that is each of these lines) contain a character variable

That is: first line needs to eb captured and stored as;

my_character_variable(1)

Second line as:

my_character_variable(2) and so on...

Can anyone help me?

That is how to open a text file and record each of the line as character variables?

Please help

15 Oct 2011 4:40 #9097

Something like this should do it:

      PROGRAM TEST

      IMPLICIT NONE

      integer*4      i
      character*200  my_char_var(100)

      open(10,err=200,file='input.txt',status='old')
      i = 1
100   read(10,'(A)',err=200,end=200)my_char_var(i)
      i = i+1
      if (i .le. 100) goto 100
200   close(10)
      end

Regards - Wilfried

15 Oct 2011 4:51 #9098

Thanks a lot.

I wrote like this and it worked - 1 problem now:

subroutine ReadingTextFileForBatchProcessing_A
      

      character*200 file_name(112)
      integer node_start(112),no_of_files
      
      open (unit = 7, file = 'C:\\Users\\u10830u\\Desktop\\files_details.txt')

           
      read(7,*)no_of_files
      
      do i=1,no_of_files
      
      read(7,*)file_name(i)
      
      enddo
      
      do i=1,no_of_files
      
      read(7,*)node_start(i)
      
      enddo
      
      
      end
      

This code works.Problem is:

If I give one of the fiel names as;

C:\Christy\geometry file\reading\my_name

in the text file, then

it detects only upto;

[i]C:\Christy\geometry[i]

This is because of the space after geometry.Why is it so?

Because the user can create folders/subfolders with a space.

Please help again!!

15 Oct 2011 5:09 #9099

Because 'space' denotes a separator with that format. You need to read the whole line, then (maybe - depends what you want it for) trim the spaces off the right-hand end of the string.

I suggest '(A256)'

E

17 Oct 2011 7:57 #9101

You need to read the whole line, then (maybe - depends what you want it for) trim the spaces off the right-hand end of the string.

I suggest '(A256)'

Please can you clarify through an example LitusSaxonicum?

shall be grateful.

17 Oct 2011 11:44 #9102

You need something like:

      subroutine ReadingTextFileForBatchProcessing_A 

      character*200 file_name(112),temp_file_name 
      integer node_start(112), no_of_files 
      
      open (unit = 7, file = 'C:\\Users\\u10830u\\Desktop\\files_details.txt')           
      read(7,*) no_of_files
 
      do i=1, no_of_files       
      read(7,'(A200)') temp_file_name
      file_name(i) = temp_file_name(1:len_trim(temp_file_name))     
      enddo 
      
      do i=1, no_of_files       
      read(7,*) node_start(i)       
      enddo 
            
      end

The '(A200)' is the format, i.e. read up to the 200 character length of the variable (I suggest 256 is better). You could alternatively surround the text strings in your datafile with single or double quotes, then the original will work. The long statement with LEN_TRIM trims the blanks off the end. FTN95 has a variety of character trimming functions.

I use an older dialect of Fortran where the A200 needs to be put in a format statement.

Why don't you buy a book on Fortran programming? They come in a range of prices. The cheapest is free: see this, for example: http://www.star.le.ac.uk/~cgp/prof77.pdf

E

23 Nov 2011 5:33 #9280

To fill a character variable by reading a complete line including all blanks until, use READ (iunit,'(A)') cstring

READ (iunit,*) cstring
will fill cstring until the first blank

Please login to reply.