Discussions related to Visual Prolog
billgates198606

unexpected end of file erro when read from external file

Unread post by billgates198606 »

i try lots of example each has erro :unexpected end of file

Code: Select all

implement main     open core   class predicates    copyLine : (inputStream I, outputStream O). clauses     copyLine(I, O) :-         Line = I:readLine(),         O:write(Line, "\n").     class predicates  onClick:()  procedure. clauses     onClick() :-         InputFile = inputStream_file::openFile("c:\\test.txt"),         OutputFile = outputStream_file::openFile("c:\\test2.txt"),         copyLine(InputFile, OutputFile),         copyLine(InputFile, OutputFile),         OutputFile:writef("a="), /* insert other data in between*/         std::repeat(),             copyLine(InputFile, OutputFile),         InputFile:endOfStream(),         !,         InputFile:close(),         OutputFile:close().   onClick() .       clauses     run():-         console::init(),       onClick(),         succeed(). % place your own code here end implement main   goal     mainExe::run(main::run).
User avatar
George
Active Member
Posts: 47
Joined: 19 Sep 2011 8:54

Code fix

Unread post by George »

The following code will works good for you, Use the same and let me know for any further support

Code: Select all

%Write data to another file only when it is not the end of stream class predicates    copyLine : (inputStream I, outputStream O). clauses     copyLine(I, O) :-         if not(I:endOfStream()) then               Line = I:readLine(),             O:write(Line, "\n")         end if.     predicates     onPushButtonClick : button::clickResponder. clauses     onPushButtonClick(_Source) = button::defaultAction:-         InputFile = inputStream_file::openFile8("E:\\test.txt"),            %use openFile8 instead of openFile         OutputFile = outputStream_file::openFile8("E:\\test2.txt"),    %use openFile8 instead of openFile         copyLine(InputFile, OutputFile),         copyLine(InputFile, OutputFile),         OutputFile:writef("a="), /* insert other data in between*/         std::repeat(),             copyLine(InputFile, OutputFile),         InputFile:endOfStream(),         !,         InputFile:close(),         OutputFile:close().     onPushButtonClick(_Source) = button::defaultAction.
Any personal support, You can reach me at georgeananth.prolog@gmail.com
Kind Regards,
George Ananth. S | Prolog Developer
georgeananth.prolog@gmail.com
+91 9791499282
User avatar
Thomas Linder Puls
VIP Member
Posts: 1398
Joined: 28 Feb 2000 0:01

Unread post by Thomas Linder Puls »

This code clearly expects that there are at least three lines in the file (copyLine is called three times before endOfStream is tested the first time).

I will definitely suggest a recursive solution instead of using a 'repeat' loop:

Code: Select all

predicates     copyTextStream : (inputStream In, outputStream Out). clauses     copyTextStream(In, Out) :-         if not(In:endOfStream()) then             Out:write(In:readline(), "\n"),             copyTextStream(In, Out)         end if.
Regards Thomas Linder Puls
PDC
User avatar
George
Active Member
Posts: 47
Joined: 19 Sep 2011 8:54

Unread post by George »

The following is the some possible way of doing this, I'd like to get some idea about Code efficiency

Case 1: Recursive solution

Code: Select all

predicates     copyTextStream : (inputStream In, outputStream Out). clauses     copyTextStream(In, Out) :-         if not(In:endOfStream()) then             Out:write(In:readline(), "\n"),             copyTextStream(In, Out)         end if.
Case 2: Using Repeat loop

Code: Select all

class predicates     copyTextStream1 : (inputStream In, outputStream Out) determ. clauses     copyTextStream1(In, Out) :-         std::repeat(),             if not(In:endOfStream()) then                 Out:write(In:readline(), "\n"),                 fail             end if,             !.
Case 3: Using Repeat with foreach loop

Code: Select all

class predicates     copyTextStream2 : (inputStream In, outputStream Out).   clauses     copyTextStream2(In, Out) :-         foreach std::repeat() and                 if In:endOfStream() then                     !,                     fail                 end if             do             Out:write(In:readline(), "\n")         end foreach.
Can you please advice, In above 3 case, which one will be the best way of doing it ? and Why ?
Kind Regards,
George Ananth. S | Prolog Developer
georgeananth.prolog@gmail.com
+91 9791499282
User avatar
Thomas Linder Puls
VIP Member
Posts: 1398
Joined: 28 Feb 2000 0:01

Unread post by Thomas Linder Puls »

As already mentioned I would go for the recursive solution.

I doubt very much that the efficiency differences are significantin any way, and therefore I would clearly choose the nice code.
Regards Thomas Linder Puls
PDC
Post Reply