Reading tabbed data 
Author Message
 Reading tabbed data

When using the read statement, is there an easy way to read tab
separated data? At the moment I can only get it to read the first data
item from each line.

Thanks

Mike

--
Michael Hughes
College of St Hild & St Bede
University of Durham
Durham DH1 1SZ
United Kingdom

Web: www.dur.ac.uk/m.r.hughes



Tue, 18 Jul 2006 01:27:30 GMT  
 Reading tabbed data

Quote:

> When using the read statement, is there an easy way to read tab
> separated data? At the moment I can only get it to read the first data
> item from each line.

Tabs are not in the fortran character set at all.  Anything that
happens with them is compiler-dependent.  You basically have 3
options.

1. Hope that maybe your compiler handles them for you.  Some might.
   Not really a good thing to count on because Some (most?  all?)
   don't. Apparently yours doesn't.

2. Get rid of the tabs one way or another and then do the read.
   The simple versions of this assume that replacing tabs with
   blanks would be ok.  If this is inadequate (for example, if
   there are embedded blanks in the fields), then life becomes
   a little more complicated.  I'll ignore that for now.
   The main 2 variants of this are

   2a) Use some external tool to get rid of them before running
       your Fortran program.

   2b) Write the code to get rid of them in Fortran.  Basically,
       read each line in as a character string, find the tabs and
       replace them by blanks, and then do an internal read from
       the fixed up character string.

3. Parse the fields yourself.  Not fundamentally difficult, but can
   be a bother.  Extracting arbitrarily-delimitted fields from a
   string is a common enough task that many people have handy
   library routines that already do this, in which case it is
   just a matter of calling the routine.

For cases where it works (i.e. no embedded blanks), 2b is probably
the simplest approach (assuming that you can't go to the original
file source and stop them from using tabs, which would be simpler).

--
Richard Maine                       |  Good judgment comes from experience;
email: my first.last at org.domain  |  experience comes from bad judgment.
org: nasa, domain: gov              |        -- Mark Twain



Tue, 18 Jul 2006 02:00:57 GMT  
 Reading tabbed data

Quote:


> > When using the read statement, is there an easy way to read tab
> > separated data? At the moment I can only get it to read the first data
> > item from each line.

> Tabs are not in the Fortran character set at all.  Anything that
> happens with them is compiler-dependent.  You basically have 3
> options.

> 1. Hope that maybe your compiler handles them for you.  Some might.
>    Not really a good thing to count on because Some (most?  all?)
>    don't. Apparently yours doesn't.

> 2. Get rid of the tabs one way or another and then do the read.
>    The simple versions of this assume that replacing tabs with
>    blanks would be ok.  If this is inadequate (for example, if
>    there are embedded blanks in the fields), then life becomes
>    a little more complicated.  I'll ignore that for now.
>    The main 2 variants of this are

>    2a) Use some external tool to get rid of them before running
>        your Fortran program.

>    2b) Write the code to get rid of them in Fortran.  Basically,
>        read each line in as a character string, find the tabs and
>        replace them by blanks, and then do an internal read from
>        the fixed up character string.

<SNIP>

The following program shows to replace a tab with a single space in a
string and may help you do 2b) above.

program xread_tab
! converts tabs to spaces for text read from standard input
implicit none
integer, parameter   :: nlen = 200, itab = 9
integer              :: i,ierr
character (len=nlen) :: text
do
   read (*,"(a)",iostat=ierr) text
   if (ierr /= 0) exit
   do i=1,nlen
      ! following line replaces each tab with a single space
      if (iachar(text(i:i)) == itab) text(i:i) = " "
   end do
   write (*,*) trim(text) ! print de-tabbed text
end do
end program xread_tab



Tue, 18 Jul 2006 06:59:08 GMT  
 Reading tabbed data


Quote:
> When using the read statement, is there an easy way to read tab
> separated data? At the moment I can only get it to read the first data
> item from each line.

If you are reading integers or floating point numbers, then most
compilers will allow you to read the data with list-directed i/o.  
However, if you are reading tab-separated text fields, without
quotes and with embedded spaces, then you will probably have to
parse the data manually.

$.02 -Ron Shepard



Tue, 18 Jul 2006 13:19:18 GMT  
 Reading tabbed data
Thanks, it's always the simple thing that works!

Quote:



>>When using the read statement, is there an easy way to read tab
>>separated data? At the moment I can only get it to read the first data
>>item from each line.

>>Thanks

>>Mike

> How about trying read(fileunit,*) a1,a2,a3, .... ?

--
Michael Hughes
College of St Hild & St Bede
University of Durham
Durham DH1 1SZ
United Kingdom

Web: www.dur.ac.uk/m.r.hughes


Wed, 19 Jul 2006 21:28:40 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Tabs, Tabs, Tabs!!

2. Read VFP data files into Clipper data files

3. reading data by using DIO read.vi (6025E board)

4. DataSocket Read is only reading default Data

5. Reading a data file from a very remote data

6. reading data by using DIO read.vi (6025E board)

7. I want write data to TestStand from LavView and Read data from LabView to TestStand

8. Read-only data and write-once data

9. help reading negative values in data file - test code and test data

10. Fortran Data Input Problem-sample data and READ stmt

11. Reading Arguments from Command Line and reading binary data

12. Formatting stdout with tabs??? Vertically lining up data

 

 
Powered by phpBB® Forum Software