 |
forums.silverfrost.com Welcome to the Silverfrost forums
|
View previous topic :: View next topic |
Author |
Message |
brucebowler Guest
|
Posted: Thu Feb 04, 2010 3:27 pm Post subject: read stops when it encounters a forward slash |
|
|
I'm attempting to read a file where each line contains, among other things, a fully qualified linux filename. When I use the following read statement, everything up until fn has the value I'd expect, but fn contains nothing. If I edit the file to remove the / characters that appear in the filename, it to is read, but without the slashes. Is there a way to get the read to read the /es?
Code: | read (10,*) v1,v2,v3,fn |
The file is formatted oddly enough that I can't really define a format statement, other than *, that will work. I'd rather not have to edit the file to replace the / with something else and then put it back in later.
Thanks!
Bruce |
|
Back to top |
|
 |
JohnHorspool
Joined: 26 Sep 2005 Posts: 270 Location: Gloucestershire UK
|
Posted: Thu Feb 04, 2010 5:10 pm Post subject: |
|
|
Bruce,
Read each line of the file as a character string. Then read the values v1, v2 and v3 from the string with an internal read and extract the filename from the string by finding its start position.
cheers,
John |
|
Back to top |
|
 |
jjgermis
Joined: 21 Jun 2006 Posts: 404 Location: N�rnberg, Germany
|
Posted: Fri Feb 05, 2010 9:37 am Post subject: |
|
|
Try something like this:
Code: | program read_line
implicit none
character(len=256) :: txt_line,txt
real :: a,b,c
integer :: i
!
! This is the line you get from a read
!
txt_line = '30.0 34.08 5789.78 c:/temp/user/'
!
! Now do an internal read
!
read(txt_line,*) a,b,c
!
! Now find the string: assuming the first blank from the right.
!
i = index (trim(txt_line), ' ', .true.)
txt = txt_line(i:len_trim(txt_line))
write(*,*) a,b,c
write(*,*) trim(txt)
end program |
|
|
Back to top |
|
 |
JohnHorspool
Joined: 26 Sep 2005 Posts: 270 Location: Gloucestershire UK
|
Posted: Fri Feb 05, 2010 11:04 am Post subject: |
|
|
Code: |
call get_substrings(txt_line,iterm,nsubs,l)
read(txt_line(iterm(1,1):iterm(2,1)),*)v1
read(txt_line(iterm(1,2):iterm(2,2)),*)v2
read(txt_line(iterm(1,3):iterm(2,3)),*)v3
fn=txt_line(iterm(1,4):iterm(2,4))
SUBROUTINE GET_SUBSTRINGS(STRING,ITERM,NSUBS,LENGTH)
C string = input character string
C length = length of character string
C nsubs = number of sub-strings
C Iterm = array of results
C iterm(1,i) = start position of ith sub-string
C iterm(2,i) = end position of ith sub-string
DIMENSION ITERM(2,100)
CHARACTER*(*) STRING
C kount = position counter
KOUNT=0
NSUBS=0
10 IF (NSUBS.EQ.100.OR.KOUNT.EQ.LENGTH) RETURN
KOUNT=KOUNT+1
IF (STRING(KOUNT:KOUNT).EQ.' ') GO TO 10
NSUBS=NSUBS+1
ITERM(1,NSUBS)=KOUNT
20 IF (KOUNT.EQ.LENGTH) THEN
ITERM(2,NSUBS)=KOUNT
RETURN
END IF
KOUNT=KOUNT+1
IF (STRING(KOUNT:KOUNT).EQ.' ') THEN
ITERM(2,NSUBS)=KOUNT-1
GO TO 10
END IF
GO TO 20
END |
|
|
Back to top |
|
 |
|
|
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
|