Re: Trouble reading bytes

new topic     » goto parent     » topic index » view thread      » older message » newer 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}}
>

The problem is that these values (less than 32) are considered "binary" 
values. If it so happens that you output a 26 (considered an EOF marker), 
then getc() will return -1 for it, although you may still be able to read 
more bytes. The solution is to open the file in binary mode ("wb" or "rb"). 
Then you can output and 8-bit value. (0 to 255) If you want to print outmore 
that that, you will have to convert the number to four bytes, using 
int_to_bytes(), or print the number as text (ie. printf(fn, "%d\n", 
{data[i]}) (remember to leave some sort of whitespace between each number, 
or they will run together)) and then read it back with get().

>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?
>
>Thanks,
>David
>

>

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu