Re: Creating A File To Be Written/Read From

new topic     » goto parent     » topic index » view thread      » older message » newer message
Andy said...

I've been going over the help files about creating a file to be written to and read from, but I'm a bit lost. I know "r" is for read, and "rb" is for a binary file, and I know the "wb" is for writing to a binary file, but I'm a bit confused. How would I make a file that had a extension of .sav, I realize I would use the "wb" for the function, but how exactly would I go about coding that?

Hi Andy, as pointed out already, it doesn't matter what extension you give it, if its your own program doing the reading and writing. If you are writing the file for somebody else's program to read, then you might have to pay more attention to the extension. This is because many programs make assumptions about the file contents/layout depending on what extension you give it.

However, assuming that the extension doesn't matter in this case, you need to decide if the file only contains ASCII text or will it contain anything else. If it only contains text, then you can write the file using the "w" open() parameter and read it using the "r" open parameter.

For example...


procedure Write_Text_Line(integer fileHandle, sequence theLine) 
    if length(theLine) = 0 then 
        puts(fileHandle, '\n') -- Just add a new line to the file 
    else 
        puts(fileHandle, theLine) -- Add the text to the file. 
        if theLine[$] != '\n' then 
            puts(fileHandle, '\n')  -- Add a new-line character if the text didn't end in one. 
        end if 
    end if 
end procedure 
 
function Read_Text_Line(integer fileHandle) 
    object fileText 
 
    fileText = gets(fileHandle) -- Get the next line of text. 
    if sequence(fileText) then 
        -- Make sure it always ends with a new-line character. 
        if fileText[$] != '\n' then 
            fileText &= '\n' 
        end if 
    end if 
 
    return fileText 
end function 
 
-- Create the file 
integer fh 
fh = open("whatever.sav", "w") 
 
-- Write some stuff to it... 
Write_Text_Line(fh, "Line One") 
Write_Text_Line(fh, "Second line") 
Write_Text_Line(fh, "The End") 
 
-- close output 
close(fh) 
 
-- Read the file  
fh = open("whatever.sav", "r") 
 
-- Get the lines... 
sequence content 
obect aLine 
content = {} 
aLine = Read_Text_Line(fh) 
while sequence(aLine) do 
    content = append(content, aLine) 
    aLine = Read_Text_Line(fh) 
end while 
 
-- close input 
close(fh) 

But if you are going to be writing atoms or sequences other than plain text, then you have to get a bit more creative. Your basic options are ...

  1. Convert your data to a text form before writing. Easy to do, but can make for larger files.
  2. Use a 'serialization' function, such as the one found in the Euphoria Database system.
  3. Create a custom file format based on your data type(s) actually used.

Note that each of these options will involve some form of data conversion before writing and after reading.

So before going any further, let's know a bit more about your data layout and we can give some more tailored advice.

Version 4 plug: In Euphoria v4, there are some new routines to make it easier to read/write files, both text and binary.

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

Search



Quick Links

User menu

Not signed in.

Misc Menu