[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: writeu, printf, readu/f 2 file
Craig Markwardt wrote:
>
> Without delving into too much detail about your specific program, I
> can say that you should pay a lot more attention to your output
> formatting. Don't assume, simply because you print it out one way, it
> can be read the same way. For example:
>
> IDL> openw, 50, 'test.dat'
> IDL> printf, 50, 1, '2 3', 4
> IDL> close, 50
> IDL> openr, 50, 'test.dat'
> IDL> a = 0 & b = '' & c = 0
> IDL> readf, 50, a, b, c
> % READF: End of file encountered. Unit: 50
> File: test.dat
> % Execution halted at: $MAIN$
>
> Hmmm, why did this fail? Well, the string B is very greedy and it
> consumes everything to the end of the line, including the "4".
>
> Mixing formatted and unformatted data in a file can be tricky. The
> problem is that when you read formatted data the file pointer can
> advance unpredictably. Better to read and write the data
> intermediately to a string buffer.
>
> Craig
>
... or use a byte array to represent strings in a binary file. I have
a little utility program available that will convert strings (or
string arrays) to byte arrays. To go back, you can simply use
String(thearray). The program is here:
http://www.mpimet.mpg.de/~schultz.martin/idl/html/src/martin_schultz/str2byte.pro
Cheers,
Martin
--
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
[[ Dr. Martin Schultz Max-Planck-Institut fuer Meteorologie [[
[[ Bundesstr. 55, 20146 Hamburg [[
[[ phone: +49 40 41173-308 [[
[[ fax: +49 40 41173-298 [[
[[ martin.schultz@dkrz.de [[
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[