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