comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: Reading a string from a file
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: Reading a string from a file [message #13817 is a reply to message #13808] Wed, 09 December 1998 00:00 Go to previous message
davidf is currently offline  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.]
[Message index]
 
Read Message
Read Message
Read Message
Previous Topic: LINKIMAGE/CALL_EXTERNAL/DLM advice
Next Topic: XDISPLAY

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Sat Oct 11 16:19:36 PDT 2025

Total time taken to generate the page: 1.19986 seconds