close (C System Call)

close is a system call that is used to close an open file descriptor.

Required Include Files

#include <unistd.h>

Function Definition

int close(int fildes);
Field Description
int fildes The file descriptor to be closed.
return value Retuns a 0 upon success, and a -1 upon failure. It is important to check the return value, because some network errors are not returned until the file is closed.

Code Snippet

Example closing an open file descriptor:

#include <stdlib.h>
#include <fcntl.h>
int main()
    size_t filedesc = open("testfile.txt", O_WRONLY | O_CREAT);
    if(filedesc < 0)
        return 1;
    if(close(filedesc) < 0)
        return 1;
    return 0;
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License