That filelength question - again .... [message #37928] |
Mon, 09 February 2004 12:47  |
jeyadev
Messages: 78 Registered: February 1995
|
Member |
|
|
Sometime back there was thread about how to determine the length
of files, but did not follow it ....
Faced suddenly with a huge bunch of files with varying lengths,
but, thankfully, the same number of columns, I was looking for
the answer to the question ... but did not find it. So the
following kludge:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
; Function to find out the number of lines in a simple
; ASCII file
function filelength , filename
cmd = 'spawn, ' + " 'wc -l " + filename + ' | sed "s/' + filename + '//"'
cmd = cmd + "', s1"
; s1 is a string array with just one element
filelength = -1
val = execute(cmd)
if(val eq 1) then filelength = atoi(s1(0)) ; command executed properly
if(val eq 0) then print, " *** Error in finding no. of lines *** "
return, filelength
end
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
where 'atoi' is just the same as the corresponding C function. It
removes leading and trailing blanks from a string and returns the
corresponding integer value.
Seems to work allright, but the fact that I did not find it in any
of the sites began to make me wonder. Am I overlooking something
here?
Have to confess that not all the error checking that ought to be
done is done here -- but then, again, I know more about my data
files :-)
For completeness, here is atoi.pro
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
; Function that mimics the C 'atoi' function
; Leading and trailing blanks are removed before conversion
; Remove leading and trailing blanks in string by using
; strtrim(str, 2)
function atoi, str
str = strtrim(str, 2) ; trim leading and trailing blanks
l = strlen(str)
m = fix(byte(str)) - 48
a = 0
for i=0,l-1 do a = a + m(i)*10^(l-1-i)
return, a
end
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
atoi.pro was written solely for dealing with filenames and
I never intended 'a' to be larger than 9999. Hope I never
have to deal with that many data files!
thanks
--
Surendar Jeyadev jeyadev@wrc.xerox.bounceback.com
Remove 'bounceback' for email address
|
|
|
|
|