View previous topic :: View next topic |
Author |
Message |
christyleomin
Joined: 08 Apr 2011 Posts: 155
|
Posted: Sat Oct 15, 2011 4:58 pm Post subject: Reading a text file |
|
|
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 |
|
 |
Wilfried Linder
Joined: 14 Nov 2007 Posts: 314 Location: D�sseldorf, Germany
|
Posted: Sat Oct 15, 2011 5:40 pm Post subject: |
|
|
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 |
|
 |
christyleomin
Joined: 08 Apr 2011 Posts: 155
|
Posted: Sat Oct 15, 2011 5:51 pm Post subject: |
|
|
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 |
|
 |
LitusSaxonicum
Joined: 23 Aug 2005 Posts: 2402 Location: Yateley, Hants, UK
|
Posted: Sat Oct 15, 2011 6:09 pm Post subject: |
|
|
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 |
|
 |
christyleomin
Joined: 08 Apr 2011 Posts: 155
|
Posted: Mon Oct 17, 2011 8:57 am Post subject: |
|
|
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 |
|
 |
LitusSaxonicum
Joined: 23 Aug 2005 Posts: 2402 Location: Yateley, Hants, UK
|
Posted: Mon Oct 17, 2011 12:44 pm Post subject: |
|
|
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 |
|
 |
johannes
Joined: 21 Jan 2011 Posts: 65 Location: Leimen, Germany
|
Posted: Wed Nov 23, 2011 6:33 pm Post subject: |
|
|
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 |
|
 |
|