file mode

This is a discussion on file mode within the C Programming forums, part of the General Programming Boards category; if I open a file that does not exist with mode r will the file pointer return as false? Example: ...

  1. #1
    International Freak
    Join Date
    May 2004
    Posts
    23

    file mode

    if I open a file that does not exist with mode r will the file pointer return as false? Example:

    Code:
    FILE *filePtr = fopen(".//file_does_not_exist.dat", "r");
    
    if(!filePtr) {
         printf("File not found.");
    } else {
         printf("File opened.");
         //other actions here
    }
    Will that return "File not found." or "File opened."?
    Once a Freak, Always a Freak.
    International Freaks

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    If a file does not exist fopen will return NULL if using mode "r"

    !NULL == true (on most compilers)
    NULL == false (again on most compilers)

    So it should get you "File not found."

  3. #3
    International Freak
    Join Date
    May 2004
    Posts
    23
    thanx
    Once a Freak, Always a Freak.
    International Freaks

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I agree with you Thantos except for the "on most compilers" part. NULL is a null pointer constant, something defined by C to be a constant expression with the value 0, or such an expression cast to void *. As we know, 0 is always false, therefore !0 is always true. So "on most compilers" should be changed to "on all compilers that claim to implement C".
    My best code is written with the delete key.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Thanks for the clarification. I thought I read that NULL wasn't required to equate to 0.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I thought I read that NULL wasn't required to equate to 0.
    It isn't required to be all bits zero, but the value is always equivalent to 0.
    My best code is written with the delete key.

  7. #7
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    So it could equate to negative zero is what you are saying ex
    Code:
    0000 0000 == NULL
    1000 0000 == NULL
    That is the only way I can see not all bits being zero and it stil equate to zero

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    10000000 != -0

  9. #9
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >10000000 != -0
    He's talking about binary representations of signed chars, but still, 1000 000 is actually -128.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I was trying to put up the actual value but I couldn't connect to the server

  11. #11
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Is that what Prelude means though?

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is that what Prelude means though?
    Something like that. I'll admit I wasn't being very clear. A null pointer can be any absolute invalid address, so it need not be all bits zero, ie. address 0x000000. The macro NULL on the other hand must evaluate to the value 0.
    My best code is written with the delete key.

  13. #13
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Quote Originally Posted by linuxdude
    Is that what Prelude means though?
    Comparing a pointer to NULL and comparing a pointer to zero are equivalent,
    so, if fp is a FILE * , then
    if(fp==0) and if(fp==NULL) are testing the same thing

    if(fp) (which is equivalent to if(fp!=0)) is the same as if(fp != NULL)

    From the Bible
    An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.55) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer
    So, 0 is a null pointer constant as are 0L, (void *) 0 etc. etc.

    Also look up the documentation on the equality operator (==) 6.9.5.5
    If one operand is a pointer and the other is a null pointer constant, the null pointer constant is converted to the type of the pointer....
    The one who says it cannot be done should never interrupt the one who is doing it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 03:49 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21