1. How do I open, read and print files?
- Posted by Dave Bunch <bunch1962 at comcast.net> Jun 22, 2007
- 551 views
I can't seem to find basic examples of how one goes about opening, selecting lines of text from, and then either displaying the lines and/or printing them to a new file. Any suggestions about where this info is found in the documentation and/or examples would be greatly appreciated. Dave
2. Re: How do I open, read and print files?
- Posted by Derek Parnell <ddparnell at bigpond.com> Jun 22, 2007
- 522 views
Dave Bunch wrote: > > I can't seem to find basic examples of how one goes about opening, > selecting lines of text from, and then either displaying the lines > and/or printing them to a new file. Any suggestions about where > this info is found in the documentation and/or examples would be > greatly appreciated. > You need to know about the open(), gets() and puts() routines. Here is an annotated example:
-- This routine copies the lines from 'FileIn' to 'FileOut'. -- 'FileIn' must exist. -- 'FileOut' is created if doesn't exist and replaced if it does exist. procedure CopyFile(sequence pFileIn, sequence pFileOut) integer lInHandle -- File Handle for input file integer lOutHandle -- File Handle for output file object lLine -- Current line read/written -- Open the input file and check that it exists. lInHandle = open(pFileIn, "r") -- "r" means open for text reading. if lInHandle = -1 then -- N.B. A handle of 1 is always the output one for the console. puts(1, "Could not open '" & pFileIn & "'\n") abort(1) -- Cancel the program. end if -- Open the output file and check that it got re/created. lOutHandle = open(pFileOut, "w") if lOutHandle = -1 then close(lInHandle) -- remembe to close input puts(1, "Could not create '" & pFileOut & "'\n") abort(1) end if lLine = gets(lInHandle) -- Get first line of text (if any) -- If 'gets' returns an atom it means -- that it has reached end-of-file. while sequence(lLine) do -- copy the line out puts(lOutHandle, lLine) -- get the next line lLine = gets(lInHandle) end while -- Close both files. close(lOutHandle) close(lInHandle) end procedure procedure main(sequence pArgs) -- Validate the command line arguments. if length(pArgs) < 4 then puts(1, "Usage: <filein> <fileout>\n") abort(1) end if -- The third arguement is the name of the input file. -- The fourth arguement is the name of the output file. CopyFile(pArgs[3], pArgs[4]) end procedure -- Start running the program, passing it the command line parameters. main(command_line())
-- Derek Parnell Melbourne, Australia Skype name: derek.j.parnell
3. Re: How do I open, read and print files?
- Posted by don cole <doncole at pacbell.net> Jun 22, 2007
- 533 views
Dave Bunch wrote: > > I can't seem to find basic examples of how one goes about opening, > selecting lines of text from, and then either displaying the lines > and/or printing them to a new file. Any suggestions about where > this info is found in the documentation and/or examples would be > greatly appreciated. > > Dave Hello Dave, Try
integer fn object line fn=open("myfile.txt","r") while 1 do line=gets(fn) if atom(line) then exit end if puts(1,line) end while
Don Cole }}}
4. Re: How do I open, read and print files?
- Posted by Jules <jdavy at dsl.pipex.com> Jun 22, 2007
- 565 views
http://www.axyp43.dsl.pipex.com/abgte.html#ch14