1. Creating A File To Be Written/Read From

Ok Guys,

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?

new topic     » topic index » view message » categorize

2. Re: Creating A File To Be Written/Read From

Andy said...

Ok Guys,

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?

Andy:
There are only two types of files binary and text.
The .xxx known as the extent is use just so a system knows
What program to use to run or use it.
On windows you associate certain file extentions to a certain program.
For example if a file ends in .txt then windows will open the file
with a text editor or .exw is run by exw.exe.
A file can have any extent type that you want as long as it is run by
a program that knows how to access it ( binary or text )
Some extents have special mean to the operating system like .exe .com .bat etc.
Bernie

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

3. Re: Creating A File To Be Written/Read From

OK,

Well I'm making my own program which needs to load and read a certain file. Which the extension I have given is .frst, anyways I need to know how I would go about doing that.

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

4. Re: Creating A File To Be Written/Read From

Andy said...

OK,

Well I'm making my own program which needs to load and read a certain file. Which the extension I have given is .frst, anyways I need to know how I would go about doing that.

Off the top of my head...

atom fn 
object line 
sequence lines 
 
-- to write it 
fn = open("filename.frst","w") 
if fn != -1 then 
   puts(fn,"What you want to put in the file here...") 
   close(fn) 
end if 
 
-- to read it with version 3.x 
fn = open("filename.frst","r") 
line = gets(fn) 
while sequence(line) do 
   lines &= line 
   line = gets(fn) 
end while 
 
-- to read it with version 4.x 
include std/io.e 
lines = read_lines( "filename.frst" ) 
-- or 
lines = read_file( "filename.frst" ) 
-- see docs for differences 

That should get you started. smile

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

5. Re: Creating A File To Be Written/Read From

euphoric said...
Andy said...

OK,

Well I'm making my own program which needs to load and read a certain file. Which the extension I have given is .frst, anyways I need to know how I would go about doing that.

Off the top of my head...

atom fn 
object line 
sequence lines 
 
-- to write it 
fn = open("filename.frst","w") 
if fn != -1 then 
   puts(fn,"What you want to put in the file here...") 
   close(fn) 
end if 
 
-- to read it with version 3.x 
fn = open("filename.frst","r") 
line = gets(fn) 
while sequence(line) do 
   lines &= line 
   line = gets(fn) 
end while 
 
-- to read it with version 4.x 
include std/io.e 
lines = read_lines( "filename.frst" ) 
-- or 
lines = read_file( "filename.frst" ) 
-- see docs for differences 

That should get you started. smile

Thanks, I'm getting a better idea now.

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

6. Re: Creating A File To Be Written/Read From

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 message » categorize

7. Re: Creating A File To Be Written/Read From

Things are a lot easier than this. If you want to load the entire file into memory you can use the methods laid out or you can use get() and print() for arbitrary EUPHORIA objects.

Shawn Pringle

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

8. Re: Creating A File To Be Written/Read From

DerekParnell said...
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.

OK, Thanks Guys, but I need to do this for a binary file. I think I can go from the text examples you have given me, but if you could show me how to do this with a binary file, then I might get a better idea.

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

Search



Quick Links

User menu

Not signed in.

Misc Menu