1. File I/O Difficulty

I am trying to read a file in for alteration on a byte-by-byte level.

-----filedemo.ex
include file.e
include input.e
clear_screen()
sequence s
integer file_i, file_o, seek_success
s =3D input("Enter input path and filename: ")
file_i =3D open(s, "rb")
sequence file_proc
integer file_length
cseek_success =3D seek(file_i,-1)
file_length =3D where(file_i)
seek_success =3D seek(file_i,0)

for x =3D 1 to file_length do
    file_proc[x] =3D getc(file_i)
end for
-----end filedemo.ex

Well, I got an error.

-----ex.err

crypt.ex:15
variable file_proc has not been assigned a value =

=2E..
-----end ex.err

Taking the hint, I then assigned to sequence file_proc to an empty sequen=
ce
before I entered the for loop.  Well, it complained that I was assigning
something to a 0-length sequence.  This makes sense, only I expected my
program would allocate storage dynamicly.

So, would someone please tell me how to resolve this error?  Or, if anyon=
e
has a better suggestion, please let me know too.

Thankx

Alan
 =

new topic     » topic index » view message » categorize

2. Re: File I/O Difficulty

At 05:32 PM 6/19/98 -0400, you wrote:
>---------------------- Information from the mail header -----------------------
>Sender:       Euphoria Programming for MS-DOS <EUPHORIA at LISTSERV.MUOHIO.EDU>
>Poster:       Alan Tu <ATU5713 at COMPUSERVE.COM>
>Subject:      File I/O Difficulty
>-------------------------------------------------------------------------------
>
>I am trying to read a file in for alteration on a byte-by-byte level.
>
>-----filedemo.ex
>include file.e
>include input.e
>clear_screen()
>sequence s
>integer file_i, file_o, seek_success
>s = input("Enter input path and filename: ")
>file_i = open(s, "rb")
>sequence file_proc
>integer file_length
>cseek_success = seek(file_i,-1)
>file_length = where(file_i)
>seek_success = seek(file_i,0)
>
>for x = 1 to file_length do
>    file_proc[x] = getc(file_i)
>end for
>-----end filedemo.ex
>
>Well, I got an error.
>
>-----ex.err
>
>crypt.ex:15
>variable file_proc has not been assigned a value =
>
>=2E..
>-----end ex.err
>
>Taking the hint, I then assigned to sequence file_proc to an empty sequen=
>ce
>before I entered the for loop.  Well, it complained that I was assigning
>something to a 0-length sequence.  This makes sense, only I expected my
>program would allocate storage dynamicly.
>
>So, would someone please tell me how to resolve this error?  Or, if anyon=
>e
>has a better suggestion, please let me know too.
>
>Thankx
>
>Alan


You are addressing specific elements of the
sequence file_proc that don't exist by using:
file_proc[x]=

if x=1 and file_proc={} then you are attempting
to write to element 1 of a zero length sequence.

To initialze the sequence for this kind of
access use:
file_proc=repeat(0,file_length)

However it might be neater to initialize
file_proc as {} and change the
"file_proc[x]=getc(file_i)" line to:

file_proc=file_proc&getc(file_i)

Graeme.
----------------------------------------------------

new topic     » goto parent     » topic index » view message » categorize

3. Re: File I/O Difficulty

>-----filedemo.ex
>include file.e
>include input.e
>clear_screen()
>sequence s
>integer file_i, file_o, seek_success
>s = input("Enter input path and filename: ")
>file_i = open(s, "rb")
>sequence file_proc
>integer file_length
>cseek_success = seek(file_i,-1)
>file_length = where(file_i)
>seek_success = seek(file_i,0)
>
>for x = 1 to file_length do
    file_proc = file_proc & getc(file_i)
     -- Was "file_proc = getc(file_i)
>end for

Another way to do this (possibly easier):

-----filedemo.ex
include input.e
clear_screen()

sequence filename
integer file_i, file_o

filename = input("Enter input path and filename: ")
file_i = open(filename, "rb")

sequence file_proc
integer character

while 1 do
    character = getc(file_i)
    if character = -1 then
        exit
    end if
    file_proc = file_proc & character
end while

-- length(file_proc) = the length of the file


It all depends on what you want to do, and how you want to do it.
(Everybody has their own coding style that they are better at handling.)


_____________________________________________________________________
You don't need to buy Internet access to use free Internet e-mail.
Get completely free e-mail from Juno at http://www.juno.com
Or call Juno at (800) 654-JUNO [654-5866]

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu