Re: Trouble reading bytes
- Posted by Derek Parnell <ddparnell at bigpond.com> Aug 01, 2003
- 461 views
----- Original Message ----- >From: dstanger at belco.bc.ca >Subject: Trouble reading bytes > > > >Im using this method to write the contents of a >sequence to a file: > >data = {{0,0,0,0,0}, > {1,1,1,1,1}, > {2,2,2,2,2}, > {3,3,3,3,3}} > >fn = open("c:\file.dat", "w") > for i = 1 to length(data) do > puts(fn, data[i]) > end for >close(fn) > >But when I read it back from the file into a sequence >using getc() sometimes a -1 is returned rather than >the number that I originally saved. Where am I going >wrong? > The "puts" routine sends BYTES to the specified device, not SEQUENCES. Using this in the example above would give you a file with a total of 20 bytes, the first 5 all zero, the next 5 all 1, etc... The "getc" routine reads BYTES from the specified device not SEQUENCES. The '-1' return value just means you got to end of file. Try this instead... constant data = {{0,0,0,0,0}, {1,1,1,1,1}, {2,2,2,2,2}, {3,3,3,3,3}} integer fn fn = open("c:\\file.dat", "w") for i = 1 to length(data) do print(fn, data[i]) puts(fn,' ') -- needed to seperate the sequences! -- I suspect this is a bug in get() that requires this. end for close(fn) fn = open("c:\\file.dat", "r") sequence res res = get(fn) while res[1] != GET_EOF do ? res[2] res = get(fn) end while close(fn) -- Derek