1. Commands Writing To Binary File

Hey Guys,

I'm using Eu 4.0 and I heard that there was an easier to write data to a binary file. I'm wondering what the commands are, or how I would go about writing data to a binary file.

new topic     » topic index » view message » categorize

2. Re: Commands Writing To Binary File

Andy said...

Hey Guys,

I'm using Eu 4.0 and I heard that there was an easier to write data to a binary file. I'm wondering what the commands are, or how I would go about writing data to a binary file.

writefile = open("mydata.dat","wb") open the new binary file puts(writefile,peek(MemoryLocation,4)) write data to it close(writefile) close it

Or did i not understand the question?

useless

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

3. Re: Commands Writing To Binary File

Andy said...

Hey Guys,

I'm using Eu 4.0 and I heard that there was an easier to write data to a binary file. I'm wondering what the commands are, or how I would go about writing data to a binary file.

If all your data is in one sequence you can simply do this ...

-- Write data to file  
include std/serialize.e 
integer size = dump(myData, theFileName)  
if size = 0 then 
    puts(1, "Failed to save data to file\n") 
else 
    printf(1, "Saved file is %d bytes long\n", size) 
end if 

And to read it back in ...

-- Read data from file  
include std/serialize.e 
sequence mydata = load(theFileName)  
if mydata[1] = 0 then 
    puts(1, "Failed to load data from file\n") 
else 
    mydata = mydata[2] -- Restored data is in second element. 
end if 
new topic     » goto parent     » topic index » view message » categorize

4. Re: Commands Writing To Binary File

DerekParnell said...
Andy said...

Hey Guys,

I'm using Eu 4.0 and I heard that there was an easier to write data to a binary file. I'm wondering what the commands are, or how I would go about writing data to a binary file.

If all your data is in one sequence you can simply do this ...

-- Write data to file  
include std/serialize.e 
integer size = dump(myData, theFileName)  
if size = 0 then 
    puts(1, "Failed to save data to file\n") 
else 
    printf(1, "Saved file is %d bytes long\n", size) 
end if 

And to read it back in ...

-- Read data from file  
include std/serialize.e 
sequence mydata = load(theFileName)  
if mydata[1] = 0 then 
    puts(1, "Failed to load data from file\n") 
else 
    mydata = mydata[2] -- Restored data is in second element. 
end if 

Still a bit confused, but I'll try a few more things. Perhaps if I give some of my code, it will help.

global integer handle 
global sequence file 
integer data 
data = 1 
file = "" 
procedure SaveFile() 
     handle = open(file,"wb") 
   if handle = -1 then 
       puts(1,"Cannot write to file!\n") 
   else 
       file = write_file(handle,data) 
   end if 
end procedure 

This is just a crude example of what I want do. Is there something wrong that I am doing, cause I get an error when I do this, or rather when I go to try it, it crashes on me.

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

5. Re: Commands Writing To Binary File

Andy said...

This is just a crude example of what I want do. Is there something wrong that I am doing, cause I get an error when I do this, or rather when I go to try it, it crashes on me.

Andy, there are few problems with that example. write_file takes a sequence of data, not just one atom/integer. You could fix that portion by writing write_file(handle, { data }).

The second problem is write_file returns an integer, 1 for success, -1 for failure. You are assigning that result to file which is a sequence, not an integer. I would probably do something like:

procedure SaveFile(sequence filename, object data) 
    if atom(data) then 
        data = { data } 
    end if 
 
    if write_file(filename, data) = -1 then 
        puts(1, "Cannot write to file!\n") 
    end if 
end procedure 
 
SaveFile("hello.bin", 1) 

The above code also gets rid of external states, and external globals both of which are troublesome once an application gets to be more than a few hundred lines of code.

Jeremy

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

6. Re: Commands Writing To Binary File

jeremy said...
Andy said...

This is just a crude example of what I want do. Is there something wrong that I am doing, cause I get an error when I do this, or rather when I go to try it, it crashes on me.

Andy, there are few problems with that example. write_file takes a sequence of data, not just one atom/integer. You could fix that portion by writing write_file(handle, { data }).

The second problem is write_file returns an integer, 1 for success, -1 for failure. You are assigning that result to file which is a sequence, not an integer. I would probably do something like:

procedure SaveFile(sequence filename, object data) 
    if atom(data) then 
        data = { data } 
    end if 
 
    if write_file(filename, data) = -1 then 
        puts(1, "Cannot write to file!\n") 
    end if 
end procedure 
 
SaveFile("hello.bin", 1) 

The above code also gets rid of external states, and external globals both of which are troublesome once an application gets to be more than a few hundred lines of code.

Jeremy

OK, thanks, this is starting to make more sense to me.

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

7. Re: Commands Writing To Binary File

Andy said...

This is just a crude example of what I want do. Is there something wrong that I am doing, cause I get an error when I do this, or rather when I go to try it, it crashes on me.

Apart from the issues that Jeremy has highlighted, the write_file() function still might not be suitable for you. That function is limited to writing out byte values from the input data. This means that the 'data' parameter must be a sequence that only consists of integers between that values 0 and 255. If it contains floating point numbers, of integers outside that range or sub-sequences, it will not write it to disk and in some cases it will even crash your program.

That is why we have the serialize() function. That function converts any Euphoria object into a sequence of bytes, that then can be safely used by write_file.

Using your example ...

procedure SaveFile(sequence filename, object data) 
   result = write_file(filename, serialize(data)) 
   if result = -1 then 
       puts(1,"Cannot save to file!\n") 
   end if 
end procedure 
 
SaveFile("mydata.dat", 1) 

And to read it back in again ...

function LoadFile(sequenced filename) 
    object data 
    data = read_file(filename) 
    if atom(data) then  
        crash("Cannot load from file") 
    else 
        data = deserialize(data) 
        if data[0] = 0 then 
            crash("Saved data was not formatted correctly") 
        else 
            data = data[2] 
        end if 
    end if 
    return data 
end function 
new topic     » goto parent     » topic index » view message » categorize

8. Re: Commands Writing To Binary File

DerekParnell said...
Andy said...

This is just a crude example of what I want do. Is there something wrong that I am doing, cause I get an error when I do this, or rather when I go to try it, it crashes on me.

Apart from the issues that Jeremy has highlighted, the write_file() function still might not be suitable for you. That function is limited to writing out byte values from the input data. This means that the 'data' parameter must be a sequence that only consists of integers between that values 0 and 255. If it contains floating point numbers, of integers outside that range or sub-sequences, it will not write it to disk and in some cases it will even crash your program.

That is why we have the serialize() function. That function converts any Euphoria object into a sequence of bytes, that then can be safely used by write_file.

Using your example ...

procedure SaveFile(sequence filename, object data) 
   result = write_file(filename, serialize(data)) 
   if result = -1 then 
       puts(1,"Cannot save to file!\n") 
   end if 
end procedure 
 
SaveFile("mydata.dat", 1) 

And to read it back in again ...

function LoadFile(sequenced filename) 
    object data 
    data = read_file(filename) 
    if atom(data) then  
        crash("Cannot load from file") 
    else 
        data = deserialize(data) 
        if data[0] = 0 then 
            crash("Saved data was not formatted correctly") 
        else 
            data = data[2] 
        end if 
    end if 
    return data 
end function 

OK, thanks

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

Search



Quick Links

User menu

Not signed in.

Misc Menu