Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

How not to read from a file?
I have this code that will read a file and return a list
of strings for me. This part works very nice. The issue
is that after i have got the list i will get an error if i
go "next". I'd expect the stream to be closed, hence
not producing any errors. Any suggestions?

I dislike posting large source code pieces so i've tried
to strip it down. If i have omitted something vital i do
appologize deeply.



% here's the fun part

goAheadList(File, WordList) :-
open(File, read, OS),
readList(OS, WordList),
close(OS).

readList(OS, []) :- at_end_of_stream(OS).

readList(OS, WordList) :-
readWord(OS, Word),
readList(OS, WordRest),
WordList = [Word|WordRest].

readWord(InStream,Chars) :-
get0(InStream,Char),
 checkCharAndReadRest(Char,Chars,InStream
).

checkCharAndReadRest(10,[],_) :- !.
checkCharAndReadRest(32,[],_) :- !.
checkCharAndReadRest(-1,[],_) :- !.
checkCharAndReadRest(end_of_file,[],_) :- !.
 checkCharAndReadRest(Char,[Char|Chars],I
nStream) :-
get0(InStream,NextChar),
 checkCharAndReadRest(NextChar,Chars,InSt
ream).


--

Kindly
Konrad
---------------------------------------------------
May all spammers die an agonizing death; have no burial places;
their souls be chased by demons in Gehenna from one room to
another for all eternity and more.

Sleep - thing used by ineffective people
as a substitute for coffee

Ambition - a poor excuse for not having
enough sense to be lazy
---------------------------------------------------

Report this thread to moderator Post Follow-up to this message
Old Post
Konrad Den Ende
11-07-04 01:56 PM


Re: How not to read from a file?
Konrad Den Ende <tmp1@viltersten.com> wrote:
> The issue
> is that after i have got the list i will get an error if i
> go "next". I'd expect the stream to be closed, hence
> not producing any errors. Any suggestions?

> goAheadList(File, WordList) :-
>    open(File, read, OS),
>    readList(OS, WordList),
>    close(OS).

> readList(OS, []) :- at_end_of_stream(OS).

> readList(OS, WordList) :-
>    readWord(OS, Word),
>    readList(OS, WordRest),
>    WordList = [Word|WordRest].

the second rule for readList applies even if you are at
the end of the stream. So upon backtracking it will attempt
to read from a closed stream.

Gj


Report this thread to moderator Post Follow-up to this message
Old Post
vannoord@let.rug.nl
11-07-04 01:56 PM


Re: How not to read from a file?
how do you do that for integers instead of chars?


Report this thread to moderator Post Follow-up to this message
Old Post
PredAlien
12-08-04 01:57 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Prolog archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:31 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.