Thread: open() and close()

  1. #1
    Registered User
    Join Date
    Feb 2005

    open() and close()

    Hi, all,

    Here i try to open a file, close it, and then try to close it again:

    int fd, result;
    fd = open("myfile", O_RDONLY);
    printf("file descriptor: %d\n", fd);
    result = close(fd);
    printf("first close: %d\n", result);
    result = close(fd);
    printf("second close: %d\n", result);
    The outputs are:
    file descriptor: 9
    first close: 0
    second close: 0

    However as we close the file at the first time, when we close it at the second time, the result returned should be -1 which is an error number. Why it still returns 0?

    Thank you!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    file descriptor: 3
    first close: 0
    second close: -1

    Which OS and compiler are you using (RH8 and GCC3.2 here)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2005
    RH9 and GCC3.2


  4. #4
    Registered User
    Join Date
    Feb 2005
    Now it runs correctly.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-27-2009, 12:46 PM
  2. Open IE, go to URL, close
    By ober in forum C++ Programming
    Replies: 10
    Last Post: 04-07-2006, 02:21 PM
  3. How to close an open port with WindowsSockets
    By codingmaster in forum Windows Programming
    Replies: 4
    Last Post: 10-03-2002, 09:52 PM
  4. Replies: 8
    Last Post: 11-21-2001, 12:13 AM
  5. Ghost in the CD Drive
    By Natase in forum A Brief History of
    Replies: 17
    Last Post: 10-12-2001, 05:38 PM