C 'try' statement

Hi, I’m trying to make a program (for learning purposes) that will on startup try to open a file called user.dat, and a file called password.dat. Then if it succeeds, it will ask for the current user name and password. If the files don’t exist, it will create them and ask for a user name and password (kind of a security feature). The problem is I don’t know how to use C’s try statement, Dev-C++ puts it in bold, so I think it is a statement/keyword, but when I try to compile, it says “undefined reference to ‘try’, first used in this function”, do I need to #include some header file? or is ‘try’ not really a statement. And if it’s not a statement, Is there any other way to do this? thanks…

The tryfinally and trycatch are C++ constructs. They do not exist in C.

They are for catching errors (aka exceptions). If you want to know more, google things like “try finally” and “call with continuation”. Also, see the C++FAQ-Lite.

Hope this helps.

[edit]Oh yeah, in C you’ll just check the result of a function and sometimes a global variable for errors. So to try to open a file say:

  FILE *f;
  if ((f = fopen( "password.dat", "rb" )) == NULL) {
    /* handle error here */
    }

Oh boy do I feel stupid! I’ve been using the ‘if fp==NULL’ setup to do other things (like printing that the file doesn’t exist) so this is pretty much exactly what I was trying to do. thank you for reminding my block head!:smiley:

Don’t feel stupid. Sounds to me like you were doing the right thing.

I combined two statements into one --which is a fairly common C idiom. Sorry.

  f = fopen( ... );
  if (f == NULL) {
    /* handle error here */
    }

I must admit that by your description I’m not sure what the difference is between the file pre-existing and having to be constructed.

1 Like

What I have now is something like:

 
FILE *u;
FILE *p;
 
u=fopen("user.dat","r");
if(fp==NULL)
{
     u=fopen("user.dat","w");
     /*etc...*/

so if it dosen’t exist it will make it, and later on in my etc… it will ask for a user name and password, if it does exist, it asks for the existing ones to change them. I got it to work, and I do feel stupid because I’ve been using the if(u==NULL) for it saying the file doesn’t exist.

Ah, then you should feel silly, because you were doing it right to begin with… :rolleyes:

Remember, programming is often confused with syntax.
Programming is manipulating things (things that have specific properties, or types).
Syntax is nothing more than the grammar of the language you use to talk to (or instruct) the computer. Your syntax is correct, but you’ve made an error.

What types of things are you manipulating?
How are they to be manipulated? And in what order?

More specifically to your example:
What kind of thing is *fp? Does it exist? If it does, what role does it play?
What kind of thing is *u? Does it exist? What role does it play?
What kind of thing is *p? Does it exist? What role does it play?

Hint: you are dealing with pointers to things, so the answers must be in terms of both the value and the thing the value references…

Answers, rot13. (cut and paste into the cipher box for the answer) [edit]but not before you try to answer the questions yourself![/edit]

sc vf cerfhznoyl n cbvagre gb n svyr fgehpgher, ohg vg vf abg qrpynerq fb arvgure vg abe gur svyr fgehpgher rkvfg.

h vf n cbvagre gb n svyr fgehpgher. h vf qrpynerq. h cbvagf gb n inyvq svyr fgehpgher. h vf orvat hfrq gb bcra/perngr gur “hfre.qng” svyr ba qvfx.

c vf n cbvagre gb n svyr fgehpgre. c vf qrpynerq. c qbrf abg cbvag gb n inyvq svyr fgehpgher. c vf abg hfrq naljurer.

Trg evq bs sc naq c naq hfr h va cynpr bs sc.

Ubcr guvf urycf.

Note that fopen may fail due to other reasons than file not existing, like insufficient permissions…
you may have a look at access() in unistd.h (posix compatible platforms) or _access() in io.h (windows) to identify the actual reason of a failure…