Re: Reading a string from a file [message #13817 is a reply to message #13808] |
Wed, 09 December 1998 00:00  |
davidf
Messages: 2866 Registered: September 1996
|
Senior Member |
|
|
Stefano Manzonetto (tesi3@a5.itba.mi.cnr.it) writes:
> I've a problem which should rather easy for you to solve. I need to
> save a string to a file and then read it without knowing before its
> length. Note that the string is just a field of a struct, so I can't get
> its length from the file's one.
If your file is an ASCII file, put the string on a line
by itself and read it like this:
mystring = ''
ReadF, lun, mystring
If your file is a binary file, make sure you are
writing an XDR file. These files will save the
length of a string along with the other "meta" data.
OpenW, lun, 'myfile.dat', /Get_Lun, /XDR
If you want to write a binary file, but for some
reason you can't use XDR format, then the best idea
is to write the length of the sting into the file.
Something like this:
WriteU, lun, StrLen(mystring), mystring
Then, when reading out:
stringLen = 0L
ReadU, lun, stringLen
mystring = String(Replicate(32B, stringLen))
ReadU, lun, mystring
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting
Phone: 970-221-0438 E-Mail: davidf@dfanning.com
Coyote's Guide to IDL Progamming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
[Note: This follow-up was e-mailed to the cited author.]
|
|
|