noob error quesion

Hey, I a newbie in unix programming.

I type the following command
if ((configfd = open(CONFIGFILE, O_CREAT | O_WRONLY)) == -1)

and the result I get from open is -1, which means error.
how can I figure out the cause of the error ?

Means that your program can not create a file specified in CONFIGFILE. Use perror to see why.

 
sprintf(buf, "open failed: %s", CONFIGFILE);
perror(buf);

Thanks a lot.
that's exactly what I was looking for.