what can cause fclose to fail? and another question: why would rename fail?
thanks
what can cause fclose to fail? and another question: why would rename fail?
thanks
Last edited by ihatejava; 10-09-2002 at 10:14 PM.
The file's been closed already? The file was deleted before you closed it?what can cause fclose to fail?
Are you just curious? Or do you have a related issue? If so, post some code.
(Use Code tags)
Demonographic rhinology is not the only possible outcome, but why take the chance
Excuse the crappy code OK this is the part that's giving me trouble. This line is executed printf("Error! Could not close all files.\n"), then it returns. f1, f2, g1, g2 should all be open, so why does fclose fail? The print statements are just tests but renameFailed is always -1. I suppose that rename would fail if fclose failed earlier, but why do they all fclose/rename fail?Code:if (fclose(f1) == EOF || fclose(f2) == EOF || fclose(g1) == EOF || fclose(g2) == EOF) { printf("Error! Could not close all files.\n"); return (0); } renameFailed = rename(file1, gFile1); printf("rename1: %d\n", renameFailed); \\ test renameFailed = rename(file2, gFile2); printf("rename2: %d\n", renameFailed); \\ test renameFailed = rename(gFile1, file1); printf("rename3: %d\n", renameFailed); \\ test renameFailed = rename(gFile2, file2); printf("rename4: %d\n", renameFailed); \\ test if (renameFailed != 0) { printf("Internal file processing error\n"); break; }
all i know is fclose returns 0 upon successfull close.
"with a gun barrel between your teeth, you speak only in vowels."
- tyler durden
yes it does. maybe i'll try testing for 0 instead of EOF...all i know is fclose returns 0 upon successfull close.
Read the fclose() man page:Originally posted by ihatejava
yes it does. maybe i'll try testing for 0 instead of EOF...
So to test fclose() to see if it failed:0 = Success
non-zero = an error occurred. errno is set to indicate the error.
>>if (fclose(fp) != 0) printf ("close failed\n");
And again for the rename() function:
Just use perror() to find out what went wrong.The rename() function returns zero if the operation succeeds, a non-zero value if it fails. When an error has occurred, errno contains a value that indicates the type of error that has been detected.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]