Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with copy_stream_data 1

Status
Not open for further replies.

renata12345

Instructor
Sep 28, 2010
8
0
0
GR
Hi there!
I'm trying to use the copy_stream_data predicate to copy user's input to a file.
The syntax is:
copy_stream_data(+StreamIn, +StreamOut)
So,SreamIn is the standard user input(the screen) and StreamOut I want it to be the file mytext.txt.
When i try writing copy_stream_data(user_input, 'mytext.txt') it gives me the following error:
ERROR: copy_stream_data/2: stream `mytext.txt' does not exist
what is it that i'm doing wrong??
I 'd really appreciate your help.

Renata.
 
First of all, the standard user input is the keyboard. The screen is the standard user output, but that's probably due to rushing :)

Ok, to read from a file or to write to a file, you need a file descriptor. You associate a file descriptor to a file when you open that file and all the subsequent file operations will be done using the file descriptor.

Code:
print_to_file :-
	open('C:/mytext.txt', write, Descriptor),
	copy_stream_data(user_input, Descriptor),
	close(Descriptor).

When you run print_to_file, it lets you type multiple atoms. You can finish with Ctrl-Z and then you can inspect the contents of your file (C:/mytext.txt in this case) - if you don't supply a full path, you have to be able to locate the file yourself depending of the user settings for your account.
 
Yeah... it's due to rushing...!

I tried your solution and it works perfectly. Thanks for your help.

Renata.
 
Hi! I'd like to pose one more question...
i have this code that deletes a lesson from a knowledge base. Because there is a posiibility that information associated with the lesson may be stored elesewhere i want them gone too..
The problem is that although the code works and does remove the lesson from base.pl does not do the same thing with the associated information in explaination.pl and optional_courses.txt... Is there a reason for that??
I'm guessing it has something to do with variable Lesson... if so, how can i fix it? thanks in advance.
Code:
delete_lesson:-
	write('*****All inputted information must be in lowercase followed by period*****'),nl,
	write('Please enter the title of the cource you wish to delete:'),nl,
	read(Lesson),nl,
	write('Now enter the semester is offerred in:'),
	read(Semester),
	nl,
	retract(offerred_in(Lesson, Semester)),
	write('Deleting the given lesson in the knowledge base...'),nl,
	tell('base.pl'),
	write(':-dynamic(offerred_in/2).'),nl,
	listing(offerred_in),
	told,
	write('phase 1/3 completed'),nl,
	retract(explain(Lesson)),
	tell('explaining.pl'),
	write(':-dynamic(explain/1).'),nl,
	listing(explain),
	told,
	write('phase 2/3 completed'),nl,
	%tell('optional_courses.txt'),
	%retract(optional_course_is(Lesson)),
	%told,
	write('phase 3/3 completed'),nl,
	write('Lesson successfully deleted.\n'),
	write('Delete another lesson?\nPress y to confirm:'),
	read(Answer),
	Answer='y',
	delete_lesson,
	nl.
 
From my point of view there is no problem with the code above, however there could be some problems elsewhere, depending how you defined offerred_in and explain.

I didn't understand exactly how does the code run for you (because I made it run fine on my machine, with both base.pl and explaining.pl being modified properly):

- does it bring up some errors after step 1/3 completed? - in this case, see if you made both offerred_in and explain dynamic
- or it runs without errors only there are no modifications in explaining.pl? - in this case, there could be a more subtle error

By the way, your code writes in 'explaining.pl' and you said something about a file 'explaination.pl'. Is it possible that you have 2 files and you are expecting the wrong file to get updated after the code runs?
 
It seems that, indeed, the code works fine. The error was coming from elsewhere.. Thanks once again.
 
I am running a tool in prolog and after the running is finished i have the fllowing result:



Graph Size statistics: Nodes = 1 Abstract Edges = 0 Cases = 1



-- Pruning outputs --


-- Print pruned output- (clock-2) MDGs options --
No printing (Return) / Statistics only (%) /
To screen (Space) / To file (FILE-NAME) :

pruned output- (clock-2)


t


Graph Size statistics: Nodes = 1 Abstract Edges = 0 Cases = 1



Error detected!

=== The Counterexample ===

-------- Assumptions --------


-------- Initial state --------
input = z3
nxa = 1
reg_output_signal = 1

-------- Clock cycle 1 --------

-- The symbolic input --

t_ls6 = 1
glitch_signal = dg
select2 = 1

-- The symbolic state --

reg_output_signal = dgp

-------- Clock cycle 2 --------

-- The symbolic input --


-- Symbolic Output --

flag = 1

=== End of counterexample ===

Generating counter example took 0.040 seconds.



=== Property checking failed ===


=== Performance statistics ===

Compiling:
Compiling individual components took: 0.300 seconds
Building the transition relations took: 0.020 seconds
Sub-total (component+transition relation): 0.320 seconds
Compiling (loading+deriving all the relations) took: 0.530 seconds
Garbage_collection: 0.000 seconds
Memory: 535924 bytes
Nodes: 495; Abstract Edges: 1;

-- Detailed statistics --
Run time: 0.530 seconds;
System time: 0.020 seconds;
Real time: 4.196 seconds

Virtual memory in use: 535924 bytes; Virtual memory free: -535924 bytes
Program space: 142732 bytes
Global stack memory: 393192 bytes; Local stack memory: 0 bytes
Global stack in use: 763424 bytes; Global stack free: -461084 bytes
Local stack in use: 604 bytes; Local stack free: -604 bytes
Trail 200
Garbage_collection 0 times: 0.000 seconds; 0 bytes freed
Global stack shift 0 times; Local stack shift 3 times; in 0.000 seconds
Number of atoms: 6; Atom space in use: 216 bytes; free: -48 bytes
Atom Garbage_collection 0 times: 0.000 seconds; 0 bytes freed

Verification:
Building initial state MDG took 0.010 seconds
Computing reachable states took : 0.030 seconds
Misc took : 0.060 seconds
Sub-total (reachability analysis) : 0.090 seconds
Verification (rechability analysis + init states generation) : 0.100 seconds
Garbage_collection: 0.020 seconds
Memory: 68764 bytes
Nodes: 169; Abstract edges : 0



but I only need to copy aprt from it. so how can I copy it from prolog to another text file.


thanks

ghaith
 
I don't quite understand, you need your Prolog program to write in a text file instead of writing to the screen? If this is the case, it's difficult to answer without having the Prolog code that generates this text.

If you are running the program from command line, you can also leave the original Prolog code as it is and instead redirect output to a file, like this:

shell_command_that_produces_output_text > file.txt

Instead of printing to the screen, the shell command will write its output to file 'file.txt'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top