Thread: quick fclose question

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    6

    quick fclose question

    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.

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    what can cause fclose to fail?
    The file's been closed already? The file was deleted before you closed it?

    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

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    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;
    		}
    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?

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    68
    all i know is fclose returns 0 upon successfull close.
    "with a gun barrel between your teeth, you speak only in vowels."
    - tyler durden

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    all i know is fclose returns 0 upon successfull close.
    yes it does. maybe i'll try testing for 0 instead of EOF...

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by ihatejava
    yes it does. maybe i'll try testing for 0 instead of EOF...
    Read the fclose() man page:
    0 = Success
    non-zero = an error occurred. errno is set to indicate the error.
    So to test fclose() to see if it failed:
    >>if (fclose(fp) != 0) printf ("close failed\n");


    And again for the rename() function:
    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.
    Just use perror() to find out what went wrong.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM