Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to read this?

Status
Not open for further replies.

mikibelavista

Technical User
Jan 18, 2012
32
< 1| 255.33 |
< 2| 32.233353 |
< 3| 27.115475 |
< 4| 24.594143 |
< 5| 32.685696 |
< 6| 21.042452 |
< 7| 47.647877 |
< 8| 92.553001 |
< 9| 96.665306 |

How to format < and |.I have tried with a2 like character but it does not work.
 
read (10,'(2x,i1,2x,f9.6)')

Norbert



The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Well it does not work.
program cz
implicit none
integer :: i
integer,dimension(240) :: x
real,dimension(240) :: y
open(10,file='s1.dat',status='old')
open(11,file='time01.dat',status='unknown')
do i=1,240
read (10,50) x(i),y(i)
50 format(2x,i3,2x,f9.6)
end do
write(11,*)y
end
forrtl: severe (64): input conversion error, unit 10, file /home/milenko/futog/s1.dat
I have to put i3 because file has 240 rows.
< 232| 940.52 |
< 233| 982.62 |
< 234| 947.38 |
< 235| 992.69 |
< 236| 954.88 |
< 237| 964.80 |
< 238| 965.99 |
< 239| 972.51 |
< 240| 975.10 |
 
I don't know about finesses of the fortran input format definition, so my solution would be something simple - like this:

mikibelavista.f95
Code:
[COLOR=#a020f0]program[/color] mikibelavista
  [COLOR=#2e8b57][b]integer[/b][/color] :: stat, num
[COLOR=#2e8b57][b]  real[/b][/color] :: var_01
  [COLOR=#0000ff]! command string - using sed to replace unwanted characters with space[/color]
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#804040][b]*[/b][/color]), [COLOR=#2e8b57][b]parameter[/b][/color] :: cmd_string [COLOR=#804040][b]=[/b][/color] [COLOR=#804040][b]&[/b][/color]
     [COLOR=#ff00ff]'sed -e "s/[<\|]/\ /g" mikibelavista0.txt > mikibelavista.txt'[/color]

  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]"Preprocessing file"[/color]
  cmd_rc [COLOR=#804040][b]=[/b][/color] system (cmd_string)
  [COLOR=#0000ff]! if command not succesfull then stop the program[/color]
  [COLOR=#804040][b]if[/b][/color] ( cmd_rc [COLOR=#804040][b].ne.[/b][/color] [COLOR=#ff00ff]0[/color] ) [COLOR=#804040][b]stop[/b][/color] [COLOR=#ff00ff]'system: Error - command failed !!!'[/color]

  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]"Reading file"[/color]

  [COLOR=#0000ff]! open input file[/color]
  [COLOR=#804040][b]open[/b][/color]([COLOR=#ff00ff]10[/color],[COLOR=#804040][b]file[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'mikibelavista.txt'[/color],[COLOR=#804040][b]status[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'old'[/color],[COLOR=#804040][b]iostat[/b][/color][COLOR=#804040][b]=[/b][/color]stat)
  [COLOR=#804040][b]if[/b][/color] (stat [COLOR=#804040][b].ne.[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color]
    [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'File cannot be opened !'[/color]
    [COLOR=#804040][b]go to[/b][/color] [COLOR=#ff00ff]99[/color]   
  [COLOR=#804040][b]end if[/b][/color]

  [COLOR=#804040][b]do[/b][/color]
    [COLOR=#804040][b]read[/b][/color]([COLOR=#ff00ff]10[/color],[COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]end[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#804040][b]99[/b][/color], [COLOR=#804040][b]iostat[/b][/color][COLOR=#804040][b]=[/b][/color]stat) num, var_01
    [COLOR=#804040][b]if[/b][/color] (stat [COLOR=#804040][b].ne.[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color]
      [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Error reading data !'[/color]
      [COLOR=#804040][b]go to[/b][/color] [COLOR=#ff00ff]99[/color]
    [COLOR=#804040][b]end if[/b][/color]
    [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'num='[/color], num, [COLOR=#ff00ff]', var_01='[/color], var_01
  [COLOR=#804040][b]end do[/b][/color]

  [COLOR=#0000ff]! close file[/color]
[COLOR=#804040][b]  99 continue[/b][/color]
  [COLOR=#804040][b]close[/b][/color] ([COLOR=#ff00ff]10[/color])
[COLOR=#a020f0]end program[/color] mikibelavista

Now, for this example datafile
mikibelavista0.txt
Code:
< 1| 255.33 |
< 2| 32.233353 |
< 3| 27.115475 |
< 4| 24.594143 |
< 5| 32.685696 |
< 6| 21.042452 |
< 7| 47.647877 |
< 8| 92.553001 |
< 9| 96.665306 |
< 232| 940.52 |
< 233| 982.62 |
< 234| 947.38 |
< 235| 992.69 |
< 236| 954.88 |
< 237| 964.80 |
< 238| 965.99 |
< 239| 972.51 |
< 240| 975.10 |
this output will be generated
Code:
$ gfortran mikibelavista.f95 -o mikibelavista

$ mikibelavista
 Preprocessing file
 Reading file
 num=           1 , var_01=   255.33000    
 num=           2 , var_01=   32.233353    
 num=           3 , var_01=   27.115475    
 num=           4 , var_01=   24.594143    
 num=           5 , var_01=   32.685696    
 num=           6 , var_01=   21.042452    
 num=           7 , var_01=   47.647877    
 num=           8 , var_01=   92.553001    
 num=           9 , var_01=   96.665306    
 num=         232 , var_01=   940.52002    
 num=         233 , var_01=   982.62000    
 num=         234 , var_01=   947.38000    
 num=         235 , var_01=   992.69000    
 num=         236 , var_01=   954.88000    
 num=         237 , var_01=   964.79999    
 num=         238 , var_01=   965.98999    
 num=         239 , var_01=   972.51001    
 num=         240 , var_01=   975.09998
Note, that the preprocessing instruction first removes the junk characters < and | from the original file, i.e. it creates the file mikibelavista.txt which looks like this:
Code:
  1  255.33  
  2  32.233353  
  3  27.115475  
  4  24.594143  
  5  32.685696  
  6  21.042452  
  7  47.647877  
  8  92.553001  
  9  96.665306  
  232  940.52  
  233  982.62  
  234  947.38  
  235  992.69  
  236  954.88  
  237  964.80  
  238  965.99  
  239  972.51  
  240  975.10
 
@mikibelavista:

next time you ask for support, I would appreciate it, if you could post a file typical for what you want to do.

Norbert

The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Well, here is another 'pure fortran' solution:

Code:
program pipe
  integer ii, indx1, indx2
  real ff
  character*30 line        
  do
    read(*,'(a30)',end=100) line
    indx1 = INDEX(line,'|')
    indx2 = INDEX(line,'|',.TRUE.)
    read(line(      2:indx1-1),*) ii
    read(line(indx1+2:indx2-1),*) ff
    write(*,*) ii,ff
  end do
  100 continue
end program pipe

compile the program and assuming to name it "pipe", execute with re-direction and pass your input file:

Code:
pipe < datafile
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top