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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a file if does not exist

Status
Not open for further replies.

Samohtvii

Programmer
Aug 2, 2012
21
AU
I have my program reading in data from a file but i want it to create a file if the given filename does not exist.

read(File),
open(File, read, S),
read(S, X),
close(S),
doStuff(X).

I have found that in the open line I should be able to have | open(File, readwrite, S),
but that doesn't work, so any other suggestions?

Thanks
 
I think that works but i need to say if the file doesn't exist create it and now do something else e.g.
If(file_does_not_exist)
/*do something*/
else
/*extract file contents*/

right now it is creating the file but can't read anything because it's empty so i need to skip that if the file didn't exist.

Thanks
 
I'm not sure to have understood everything.
You can try
Code:
(open(File, read, S) 
-> % do stuff when open for readind succeeds
   ...
   ...
; open(File, write, S),
  % do stuff when open for readin fails
  ...
  ...
)
 
Hmm that still doesn't work. It finds it if the file exists but not if it doesn't. I am using gprolog
 
Samohtvii said:
It finds it if the file exists but not if it doesn't
Looks evident, so the code open(File, write, S),... should be processed if the file can't be open for reading (the file doesn't exist).
I only use SWI-Prolog.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top