replica nfl jerseysreplica nfl jerseyssoccer jerseyreplica nfl jerseys forums.silverfrost.com :: View topic - Reading a text file
forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Reading a text file

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General
View previous topic :: View next topic  
Author Message
christyleomin



Joined: 08 Apr 2011
Posts: 155

PostPosted: Sat Oct 15, 2011 4:58 pm    Post subject: Reading a text file Reply with quote

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
Back to top
View user's profile Send private message
Wilfried Linder



Joined: 14 Nov 2007
Posts: 314
Location: D�sseldorf, Germany

PostPosted: Sat Oct 15, 2011 5:40 pm    Post subject: Reply with quote

Something like this should do it:

Code:
      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
Back to top
View user's profile Send private message
christyleomin



Joined: 08 Apr 2011
Posts: 155

PostPosted: Sat Oct 15, 2011 5:51 pm    Post subject: Reply with quote

Thanks a lot.

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


Code:
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!!
Back to top
View user's profile Send private message
LitusSaxonicum



Joined: 23 Aug 2005
Posts: 2402
Location: Yateley, Hants, UK

PostPosted: Sat Oct 15, 2011 6:09 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
christyleomin



Joined: 08 Apr 2011
Posts: 155

PostPosted: Mon Oct 17, 2011 8:57 am    Post subject: Reply with quote

Quote:
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.
Back to top
View user's profile Send private message
LitusSaxonicum



Joined: 23 Aug 2005
Posts: 2402
Location: Yateley, Hants, UK

PostPosted: Mon Oct 17, 2011 12:44 pm    Post subject: Reply with quote

You need something like:

Code:
      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
Back to top
View user's profile Send private message
johannes



Joined: 21 Jan 2011
Posts: 65
Location: Leimen, Germany

PostPosted: Wed Nov 23, 2011 6:33 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group