reading format data [message #6503] |
Wed, 10 July 1996 00:00  |
Hon Yee
Messages: 2 Registered: July 1996
|
Junior Member |
|
|
in my data file there are empty lines. I read data as structures, and
and empty line gives values of zeros. How can avoid empty lines, or
ignore them soon i don't have returning zeros. could someone help
out,please?. Thank in advance.
Hon
|
|
|
Re: reading format data [message #6582 is a reply to message #6503] |
Thu, 11 July 1996 00:00  |
hahn
Messages: 108 Registered: November 1993
|
Senior Member |
|
|
Hon Yee <yeek@boulder.colorado.edu> wrote:
> in my data file there are empty lines. I read data as structures, and
> and empty line gives values of zeros. How can avoid empty lines, or
> ignore them soon i don't have returning zeros. could someone help
> out,please?. Thank in advance.
There are several way to approach a solution, depending on the size
of your data sets and the amount of disk space.
Assumption 1: Each record is delimited by LF or CR LF or some other
delimiter. Then an empty record will be made up of two delimiters.
a) You can use some stream editor to replace two adjacent delimiter
by one. However, you have to the data twice on the disk during
compression.
b) If the data in the file is printable, you read your data set record
by record into a string variable. If the string variable is not
empty you read your structure from the string.
c) If the data is binary, no conversion will be required. Thus you
cannot READ from the buffer but need to transport the data
without conversion.
Assumption 2: There are no delimiters but fixed length records.
a) You can use some stream editor to replace the fixed length
blank records by nothing. This again requires storing the data
set twice on the disk.
b) You read your data set record by record into a string variable.
if the string variable is not empty you backspace the file and read
into the structure.
Example for Assuption 1 solution b:
stru = { ....... } ; your data structure
OpenR, iunit, dsn, /get_lun ; Open input file
buffer = '*'
while not eof(iunit) do begin
readf, iunit, buffer
if StrLen ( buffer ) gt 1 then ReadS, buffer, stru
end
Example for Assumption 1 solution c:
stru = { a:0L, b:fltarr(10), c:0B, d:'####' } ; your data structure
OpenR, iunit, dsn, /get_lun ; Open input file
buffer = '*'
while not eof(iunit) do begin
readf, iunit, buffer
if StrLen ( buffer ) gt 1 then begin
bbuffer = Byte(buffer)
stru.a = long(bbuffer,0)
stru.b = float(bbuffer(4:43),0,10)
stru.c = bbuffer(44)
stru.d = string(bbuffer(45:48))
end
end
Example for Assumption 2 b)
stru = { ....... } ; your data structure
OpenR, iunit, dsn, /get_lun ; Open input file
buffer = '*'
while not eof(iunit) do begin
point_lun, -iunit, pos ; remember current file position
readf, iunit, buffer
if StrLen ( buffer ) gt 1 then begin
point_lun, iunit, pos ; goto remembered file position
ReadU, iunit, stru
endif
end
Hope, this helps
Norbert
|
|
|
Re: reading format data [message #6585 is a reply to message #6503] |
Thu, 11 July 1996 00:00  |
David Foster
Messages: 341 Registered: January 1996
|
Senior Member |
|
|
Hon Yee <yeek@boulder.colorado.edu> wrote:
>
> in my data file there are empty lines. I read data as structures, and
> and empty line gives values of zeros. How can avoid empty lines, or
> ignore them soon i don't have returning zeros. could someone help
> out,please?. Thank in advance.
>
You might try reading the data file as an array of strings,
remove the blank lines, and then read the contents of the
string array into your structure, if that's what you want.
I'm sending you via email a routine called FILE_STRARR.PRO that
reads the contents of a file into a string array. It's written
for UNIX systems, but you could modify if for Mac or PC easily.
Using this, you could say:
string_array = FILE_STRARR( filename )
non_blank = where( string_array ne '' )
if ( non_blank(0) ne -1 ) then $
string_array = string_array( non_blank )
Hope this helps.
Dave Foster
foster@bial1.ucsd.edu
|
|
|