1. Trouble reading bytes
- Posted by dstanger at belco.bc.ca
Aug 01, 2003
This is a multi-part message in MIME format.
------=_NextPart_000_0007_01C35776.DE946350
charset="iso-8859-1"
Im using this method to write the contents of a sequence to a file:
data =3D {{0,0,0,0,0},
{1,1,1,1,1},
{2,2,2,2,2},
{3,3,3,3,3}}=20
fn =3D open("c:\file.dat", "w")
for i =3D 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=20
------=_NextPart_000_0007_01C35776.DE946350
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Diso-8859-1" =
http-equiv=3DContent-Type>
<META content=3D"MSHTML 5.00.3103.1000" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>Im using this method to write the =
contents of a=20
sequence to a file:</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>data =3D {{0,0,0,0,0},</FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2> &nbs=
p;{1,1,1,1,1},</FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2> &nbs=
p;{2,2,2,2,2},</FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2> &nbs=
p;{3,3,3,3,3}} </FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>fn =3D open("c:\file.dat",=20
"w")<BR> for i =3D 1 to length(data)=20
do<BR> puts(fn,=20
data[i])<BR> end =
for<BR>close(fn)<BR></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>But when I read it back from the =
file into a=20
sequence using getc() sometimes a -1 is returned rather than =
the=20
number that I originally saved. Where am I going =
wrong?</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>Thanks,</FONT></DIV>
------=_NextPart_000_0007_01C35776.DE946350--
2. Re: Trouble reading bytes
- Posted by 1evan at sbcglobal.net
Aug 01, 2003
From the docs:
Comments: When you output a sequence of bytes it must not have any
(sub)sequences within it. It must be a sequence of atoms only.
(Typically a string of ASCII codes).
Avoid outputting 0's to the screen or to standard output. Your output
may get truncated.
data is a sequence of 5 subsequences.
Something like:
sequence data
object fn
data = {{0,0,0,0,0},
{1,1,1,1,1},
{2,2,2,2,2},
{3,3,3,3,3}}
fn = open("file.dat", "w")
for i = 1 to length(data) do
for j = 1 to length(data[i]) do
printf(fn,"%d",data[i][j])
end for
end for
close(fn)
will get you closer to what you are looking for.
dstanger at belco.bc.ca wrote:
>
> 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?
>
> Thanks,
> David
>
> --^^---------------------------------------------------------------
> This email was sent to: 1evan at sbcglobal.net
>
>
> TOPICA - Start your own email discussion group. FREE!
>
3. Re: Trouble reading bytes
>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
>
>
4. Re: Trouble reading bytes
On Fri, Aug 01, 2003 at 12:36:18AM +0000, Al Getz wrote:
>
>
> dstanger at belco.bc.ca wrote:
> > 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?
> >
> > Thanks,
> > David
> >
>
> Dont you use 'get()' for that?
>
> Take care,
> Al
>
No. He is using puts() to output the data, not print() or ?().
-1 means it has reached the end of file. My guess: use mode "wb" instead of
"w" to write the file, and use mode "rb" to read it back (since the data
appears to be binary as opposed to text).
jbrown
>
>
> TOPICA - Start your own email discussion group. FREE!
>
>
--
/"\ ASCII ribbon | http://www.geocities.com/jbrown1050/
\ / campain against | Linux User:190064
X HTML in e-mail and | Linux Machine:84163
/*\ news, and unneeded MIME | http://verify.stanford.edu/evote.html
5. Re: Trouble reading bytes
----- 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
6. Re: Trouble reading bytes
>From: Derek Parnell <ddparnell at bigpond.com>
> >From: dstanger at belco.bc.ca
<snip>
>The "puts" routine sends BYTES to the specified device, not SEQUENCES.
You mean I can't do puts(1, "a sequence that happens to be a string") ??
That is a sequence after all.
I believe Derek meant:
The "puts" routine sends a sequence of BYTES to the specified device, not a
sequence of SEQUENCES.
which, BTW, David was not doing...
<snip again>
>
>--
>Derek
>
7. Re: Trouble reading bytes
dstanger at belco.bc.ca wrote:
> 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?
1. Use \\ instead of \ inside a string.
2. You should use "rb" and "wb" when you are
writing non-text data, otherwise DOS/Windows will
do strange things with numbers like 10, 13 and 26.
(LF, CR and control-Z)
Regards,
Rob Craig
Rapid Deployment Software
http://www.RapidEuphoria.com
8. Re: Trouble reading bytes
----- Original Message -----=20
From: "Elliott Sales de Andrade" <quantum_analyst at hotmail.com>
To: "EUforum" <EUforum at topica.com>
Subject: Re: Trouble reading bytes
>=20
>=20
> >From: Derek Parnell <ddparnell at bigpond.com>
> > >From: dstanger at belco.bc.ca
> <snip>
>=20
> >The "puts" routine sends BYTES to the specified device, not =
SEQUENCES.
>=20
> You mean I can't do puts(1, "a sequence that happens to be a string") =
??
> That is a sequence after all.
>=20
> I believe Derek meant:
>=20
> The "puts" routine sends a sequence of BYTES to the specified device, =
not a=20
> sequence of SEQUENCES.
>=20
> which, BTW, David was not doing...
>=20
No, that is NOT what I meant at all. I meant what I said.=20
puts() ONLY WRITES BYTES!=20
Try this...
puts(fn, {400,500,600,700})
You should get written out to 'fn' 4 BYTES - not a Sequence containing 4 =
bytes. The four bytes are #90, #F4, #58, #BC.
> You mean I can't do puts(1, "a sequence that happens to be a string") =
??
Of course you can. But each element is converted to a byte value first =
before outputting it.
If you tried to do puts(fn, {"derek","parnell"}) it would fail with the =
good ole message "sequence found inside character string".
To repeat myself, puts() only writes out bytes. But let me quote the Eu =
manual...
Syntax: puts(fn, x) =20
Description: Output, to file or device fn, a single byte (atom) or =
sequence of bytes. The low order 8-bits of each value is actually sent =
out. If fn is the screen you will see text characters displayed. =20
Comments: When you output a sequence of bytes it must not have any =
(sub)sequences within it. It must be a sequence of atoms only. =
(Typically a string of ASCII codes). =20
So sorry, puts() cannot write structured data like a sequence. To do =
that you need to either use the print() routine or develop your own =
custom built method.
--=20
Derek
9. Re: Trouble reading bytes
> ----- Original Message -----=20
> >From: dstanger at belco.bc.ca=20
> >To: EUforum=20
> >Sent: Friday, August 01, 2003 8:17 AM
> >Subject: Trouble reading bytes
> >
> >
> >Im using this method to write the contents of a=20
> >sequence to a file:
> >
> >data =3D {{0,0,0,0,0},
> > {1,1,1,1,1},
> > {2,2,2,2,2},
> > {3,3,3,3,3}}=20
> >
> >fn =3D open("c:\file.dat", "w")
> > for i =3D 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?
> >
>=20
Of course, if you are actually trying to use bytes rather than =
sequences, this is one way to read them back in...if you know the =
existing structure' of the data on disk, that is...
fn =3D open("c:\\file.dat", "rb")
object res =20
res =3D repeat(-1,5)
for i =3D 1 to 4 do
for j =3D 1 to 5 do
res[j] =3D getc(fn)
end for
? res
end for
close(fn)
--=20
Derek
10. Re: Trouble reading bytes
----- Original Message -----
From: "Elliott Sales de Andrade" <quantum_analyst at hotmail.com>
To: "EUforum" <EUforum at topica.com>
Subject: Re: Trouble reading bytes
Now that I applied a bit more thought to what you were saying, Elliott, I can
see that you are describing the INPUT to the puts() routine whereas I was
describing the output from it.
--
Derek.
11. Re: Trouble reading bytes
On Fri, 1 Aug 2003 17:14:41 +1000, Derek Parnell
<ddparnell at bigpond.com> wrote:
<snip>
>puts() ONLY WRITES BYTES!=20
>
>Try this...
>
> puts(fn, {400,500,600,700})
>
>You should get written out to 'fn' 4 BYTES - not a Sequence containing 4=
bytes. The four bytes are #90, #F4, #58, #BC.
And try this:
sequence d
d=3D{97,353,609,1121,2145,4193}
puts(1,d) -- "aaaaaa" !
The moral is never use puts() to store anything other than character
data. Numbers get clobbered (as documented, but worth repeating).
I forget the original poster, but I'd recommend using database.e. The
following is a little starter:
include database.e -- see C:\EUPHORIA\HTML\DATABASE.HTM
sequence data
data =3D {{0,0,0,0,0},
{1,1,1,1,1},
{2,2,2,2,2},
{3,3,3,3,3}}=20
integer recordnumber
if db_open("data.edb",DB_LOCK_NO)=3DDB_OPEN_FAIL then
if db_create("data.edb",DB_LOCK_NO)!=3DDB_OK then
puts(1,"error creating database")
abort(0)
end if
end if
if db_select_table("data")!=3DDB_OK then
if db_create_table("data")!=3DDB_OK then
puts(1,"error creating data table")
abort(0)
end if
end if
recordnumber=3Ddb_find_key(1)
if recordnumber<0 then
if db_insert(1,data)!=3DDB_OK then
puts(1,"error writing data")
abort(0)
end if
else
db_replace_data(recordnumber,data)
end if
?data
?db_record_data(db_find_key(1))
db_close()
if getc(0) then end if
12. Re: Trouble reading bytes
- Posted by eugtk at yahoo.com
Aug 01, 2003
--- dstanger at belco.bc.ca wrote:
>
> 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?
You're going about this the hard way:
If, as you state, you want to save and retrieve
data _as a sequence_, then try this:
fn = open("file.dat", "w")
print(fn,data)
close(fn)
That saves the sequence as written:
{{0,0,0,0,0},{1,1,1,1,1},{2,2,2,2,2},{3,3,3,3,3}}
To read it back in _as a sequence_, use this:
fn = open("file.dat","r")
data = get(fn) -- need to include get.e
data = data[2]
close(fn)
? data
{
{0,0,0,0,0},
{1,1,1,1,1},
{2,2,2,2,2},
{3,3,3,3,3}
}
Regards
Irv
13. Re: Trouble reading bytes
Ah, the joy of Topica. What happened to the e-mail that I sent in reply
that actually had anything important in it??
>From: Derek Parnell <ddparnell at bigpond.com>
>Subject: Re: Trouble reading bytes
>
>
>----- Original Message -----
>From: "Elliott Sales de Andrade" <quantum_analyst at hotmail.com>
>To: "EUforum" <EUforum at topica.com>
>Subject: Re: Trouble reading bytes
>
> >
> > >From: Derek Parnell <ddparnell at bigpond.com>
> > > >From: dstanger at belco.bc.ca
> > <snip>
> >
> > >The "puts" routine sends BYTES to the specified device, not SEQUENCES.
> >
> > You mean I can't do puts(1, "a sequence that happens to be a string") ??
> > That is a sequence after all.
> >
> > I believe Derek meant:
> >
> > The "puts" routine sends a sequence of BYTES to the specified device,
>not a
> > sequence of SEQUENCES.
> >
> > which, BTW, David was not doing...
> >
>
>No, that is NOT what I meant at all. I meant what I said.
>
>puts() ONLY WRITES BYTES!
>
>Try this...
>
> puts(fn, {400,500,600,700})
>
>You should get written out to 'fn' 4 BYTES - not a Sequence containing 4
>bytes. The four bytes are #90, #F4, #58, #BC.
>
> > You mean I can't do puts(1, "a sequence that happens to be a string") ??
>
>Of course you can. But each element is converted to a byte value first
>before outputting it.
>
>
>--
>Derek
>
14. Re: Trouble reading bytes
On Fri, Aug 01, 2003 at 04:13:55PM -0400, Elliott Sales de Andrade wrote:
>
>
> Ah, the joy of Topica. What happened to the e-mail that I sent in reply
> that actually had anything important in it??
>
I got it.
jbrown
> >From: Derek Parnell <ddparnell at bigpond.com>
> >Reply-To: EUforum at topica.com
> >To: EUforum <EUforum at topica.com>
> >Subject: Re: Trouble reading bytes
> >Date: Fri, 1 Aug 2003 17:14:41 +1000
> >
> >
> >----- Original Message -----
> >From: "Elliott Sales de Andrade" <quantum_analyst at hotmail.com>
> >To: "EUforum" <EUforum at topica.com>
> >Sent: Friday, August 01, 2003 3:48 PM
> >Subject: Re: Trouble reading bytes
> >
> >>
> >> >From: Derek Parnell <ddparnell at bigpond.com>
> >> > >From: dstanger at belco.bc.ca
> >> <snip>
> >>
> >> >The "puts" routine sends BYTES to the specified device, not SEQUENCES.
> >>
> >> You mean I can't do puts(1, "a sequence that happens to be a string") ??
> >> That is a sequence after all.
> >>
> >> I believe Derek meant:
> >>
> >> The "puts" routine sends a sequence of BYTES to the specified device,
> >not a
> >> sequence of SEQUENCES.
> >>
> >> which, BTW, David was not doing...
> >>
> >
> >No, that is NOT what I meant at all. I meant what I said.
> >
> >puts() ONLY WRITES BYTES!
> >
> >Try this...
> >
> > puts(fn, {400,500,600,700})
> >
> >You should get written out to 'fn' 4 BYTES - not a Sequence containing 4
> >bytes. The four bytes are #90, #F4, #58, #BC.
> >
> >> You mean I can't do puts(1, "a sequence that happens to be a string") ??
> >
> >Of course you can. But each element is converted to a byte value first
> >before outputting it.
> >
> >
> >--
> >Derek
> >
>
>
>
> TOPICA - Start your own email discussion group. FREE!
>
>
--
/"\ ASCII ribbon | http://www.geocities.com/jbrown1050/
\ / campain against | Linux User:190064
X HTML in e-mail and | Linux Machine:84163
/*\ news, and unneeded MIME | http://verify.stanford.edu/evote.html
15. Re: Trouble reading bytes
On Fri, Aug 01, 2003 at 01:05:14PM +1000, Derek Parnell wrote:
<Snip>
>
> 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.
Technically its not a bug. get() wants whitespace to make clear that those
values are seperate.
> 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
>
Here is a version that is simpler.
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")
print(fn, data)
close(fn)
fn = open("c:\\file.dat", "r")
sequence res
res = get(fn)
if res[1] = GET_SUCCESS then
--we have the data
else
--error
end while
close(fn)
>
>
> TOPICA - Start your own email discussion group. FREE!
>
>
jbrown
--
/"\ ASCII ribbon | http://www.geocities.com/jbrown1050/
\ / campain against | Linux User:190064
X HTML in e-mail and | Linux Machine:84163
/*\ news, and unneeded MIME | http://verify.stanford.edu/evote.html