open() and close()

This is a discussion on open() and close() within the C Programming forums, part of the General Programming Boards category; Hi, all, Here i try to open a file, close it, and then try to close it again: Code: int ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    30

    open() and close()

    Hi, all,

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

    Code:
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    *shrugs*
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

    Strange...

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

    Thanks!

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-20-2001, 11:13 PM
  5. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 05:38 PM

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