Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: fopen



Ah, great stuff Cameron, I really like the way you showed the ability to put the errors in Human readable form, thanks

 

Yes I must be losing my mind, because I could have sworn, previously, my code was creating the new directory, but I might have just added manually at the beginning of this project and assumed it was created each time when it was not. As you might tell I am new to the Unix environment. J

 

Two questions is you do not mind.

 

1.)     Does mkdir allow be to specify a multiple directory path to be installed, or do I have to do some kind of loop to install it. i.e. mkdir(“/otr/mydir”, 0755)?

 

2.)     What is x-man-page that everyone is referring to? I know about man in a terminal but I am not familiar with the ‘x-man-page’ notation.

 

Thanks so much

Carl

 

-----Original Message-----
From: Cameron Kerr [mailto:email@hidden]
Sent: Thursday, June 30, 2005 7:12 PM
To: Carl Smith
Cc: email@hidden
Subject: Re: fopen

 

First of all, one of the main problems you have is that you aren't checking your errors. UNIX, unlike Mac OS, has had, for many many years, a simple way of printing out the actual error, in a way a human can easily understand. None of the "error -12345" business.

 

The correct way to check the return code from fopen (check the Return section of the manual-page to see how best to check for errors) is as follows.

 

FILE *fp;

char *filename = "xxxx";

 

if( (fp = fopen( filename, "w" )) == NULL ) {

    perror("Could not open filename");

    // either return with an error code, or exit(EXIT_FAILURE)

}

 

x-man-page://perror, which is part of the stdio library, will  output to stderr, the string given, followed by a colon and space, and the human-readable string that relates to errno, such as "No such file or directory" (which is what you would get if errno == ENOENT)

 

If you want more control, you can use x-man-page://strerror

 

You should always check your return codes.

 

One other problem you have is that you're assuming a folder will be created for you, which fopen will not do.

 

-- 

Cameron Kerr

Telecommunications Teaching Fellow & SysAdmin

email@hidden

 



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.