×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Creating a file if does not exist

Creating a file if does not exist

Creating a file if does not exist

(OP)
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

RE: Creating a file if does not exist

Try update instead of readwrite.

RE: Creating a file if does not exist

(OP)
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

RE: Creating a file if does not exist

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
  ...
  ...
) 

RE: Creating a file if does not exist

(OP)
Hmm that still doesn't work. It finds it if the file exists but not if it doesn't. I am using gprolog

RE: Creating a file if does not exist

Quote (Samohtvii)

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close