Skip to main content
Engineering LibreTexts

13.5: File Open Operations

  • Page ID
    19943
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    In order to perform file operations such as read and write, the file must first be opened. There are two file open operations, open and open/create. Each of the two open operations are explained in the following sections.

    After the file is opened, in order to perform file read or write operations the operating system needs detailed information about the file, including the complete status and current read/write location. This is necessary to ensure that read or write operations pick up where they left off (from last time).

    If the file open operation fails, an error code will be returned. If the file open operation succeeds, a file descriptor is returned. This applies to both high-level languages and assembly code.

    The file descriptor is used by the operating system to access the complete information about the file. The complete set of information about an open file is stored in an operating system data structure named File Control Block (FCB). In essence, the file descriptor is used by the operating system to reference the correct FCB. It is the programmer's responsibility to ensure that the file descriptor is stored and used correctly.

    File Open

    The file open requires that the file exists in order to be opened. If the file does not exist, it is an error.

    The file open operation also requires the parameter flag to specify the access mode. The access mode must include one of the following:

    • Read-Only Access → O_RDONLY

    • Write-Only Access → O_WRONLY

    • Read/Write Access → O_RDWR

    One of these access modes must be used. Additional access modes may be used by OR'ing with one of these. This might include modes such as append mode (which is not addressed in this text). Refer to Appendix C, System Services for additional information regarding the file access modes.

    The arguments for the file open system service are as follows:

    Register

    SYS_open

    rax

    Call code = SYS_open (2)

    rdi

    Address of NULL terminated file name string

    rsi

    File access mode flag

    Assuming the following declarations:

        SYS_open         equ        2          ; file open
    
        O_RDONLY         equ        000000q    ; read only
        O_WRONLY         equ        000001q    ; write only
        O_RDWR           equ        000002q    ; read and write
    

    After the system call, the rax register will contain the return value. If the file open operation fails, rax will contain a negative value (i.e., < 0). The specific negative value provides an indication of the type of error encountered. Refer to Appendix C, System Services for additional information on error codes. Typical errors might include invalid file descriptor, file not found, or file permissions error.

    If the file open operation succeeds, rax contains the file descriptor. The file descriptor will be required for further file operations and should be saved.

    Refer to the section on Example File Read for a complete example that opens a file.

    File Open/Create

    A file open/create operation will create a file. If the file does not exist, a new file will be created. If the file already exists, it will be erased and a new file created. Thus, the previous contents of the file will be lost.

    A file access mode must be specified. Since the file is being created, the access mode must include the file permissions that will be set when the file is created. This would include specifying read, write, and/or execute permissions for the user, group, or world as is typical for Linux file permissions. The only permissions addressed in this example are for the user or owner of the file. As such, other users (i.e., using other accounts) will not be able to access the file our program creates. Refer to Appendix C, System Services for additional information regarding the file access modes.

    The arguments for the file open/create system service are as follows:

    Register

    SYS_creat

    rax

    Call code = SYS_creat (85)

    rdi

    Address of NULL terminated file name string

    rsi

    File access mode flag

    Assuming the following declarations:

    SYS_creat        equ    85         ; file open
    
    O_CREAT          equ    0x40
    O_TRUNC          equ    0x200
    O_APPEND         equ    0x400
    
    S_IRUSR          equ    00400q     ; owner, read permission
    S_IWUSR          equ    00200q     ; owner, write permission
    S_IXUSR          equ    00100q     ; owner, execute permission
    

    The file status flags “S_IRUSR | S_IWUSR” would allow simultaneous read and write, which is typical. The “|” is a logical OR operation, thus combining the selections.

    If the file open/create operation does not succeed, a negative value is returned in the rax register. If file open/create operation succeeds, a file descriptor is returned. The file descriptor is used for all subsequent file operations.

    Refer to the section on Example File Write for a complete example file open/create.


    This page titled 13.5: File Open Operations is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Ed Jorgensen.

    • Was this article helpful?