Movatterモバイル変換


[0]ホーム

URL:


man7.org > Linux >man-pages

Linux/UNIX system programming training


fopen(3p) — Linux manual page

PROLOG |NAME |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |EXAMPLES |APPLICATION USAGE |RATIONALE |FUTURE DIRECTIONS |SEE ALSO |COPYRIGHT

FOPEN(3P)               POSIX Programmer's ManualFOPEN(3P)

PROLOG        top

       This manual page is part of the POSIX Programmer's Manual.  The       Linux implementation of this interface may differ (consult the       corresponding Linux manual page for details of Linux behavior), or       the interface may not be implemented on Linux.

NAME        top

       fopen — open a stream

SYNOPSIS        top

       #include <stdio.h>       FILE *fopen(const char *restrictpathname, const char *restrictmode);

DESCRIPTION        top

       The functionality described on this reference page is aligned with       the ISO C standard. Any conflict between the requirements       described here and the ISO C standard is unintentional. This       volume of POSIX.1‐2017 defers to the ISO C standard.       Thefopen() function shall open the file whose pathname is the       string pointed to bypathname, and associates a stream with it.       Themode argument points to a string. If the string is one of the       following, the file shall be opened in the indicated mode.       Otherwise, the behavior is undefined.r orrb       Open file for reading.w orwb       Truncate to zero length or create file for writing.a orab       Append; open or create file for writing at end-of-                     file.r+ orrb+ orr+b                     Open file for update (reading and writing).w+ orwb+ orw+b                     Truncate to zero length or create file for update.a+ orab+ ora+b                     Append; open or create file for update, writing at                     end-of-file.       The character'b'shall have no effect, but is allowed for ISO C       standard conformance.  Opening a file with read mode (r as the       first character in themode argument) shall fail if the file does       not exist or cannot be read.       Opening a file with append mode (a as the first character in themode argument) shall cause all subsequent writes to the file to be       forced to the then current end-of-file, regardless of intervening       calls tofseek().       When a file is opened with update mode ('+'as the second or third       character in themode argument), both input and output may be       performed on the associated stream. However, the application shall       ensure that output is not directly followed by input without an       intervening call tofflush() or to a file positioning function       (fseek(),fsetpos(), orrewind()), and input is not directly       followed by output without an intervening call to a file       positioning function, unless the input operation encounters end-       of-file.       When opened, a stream is fully buffered if and only if it can be       determined not to refer to an interactive device. The error and       end-of-file indicators for the stream shall be cleared.       Ifmode isw,wb,a,ab,w+,wb+,w+b,a+,ab+, ora+b, and the       file did not previously exist, upon successful completion,fopen()       shall mark for update the last data access, last data       modification, and last file status change timestamps of the file       and the last file status change and last data modification       timestamps of the parent directory.       Ifmode isw,wb,a,ab,w+,wb+,w+b,a+,ab+, ora+b, and the       file did not previously exist, thefopen() function shall create a       file as if it called thecreat() function with a value appropriate       for thepath argument interpreted frompathname and a value of       S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH for themode argument.       Ifmode isw,wb,w+,wb+, orw+b, and the file did previously       exist, upon successful completion,fopen() shall mark for update       the last data modification and last file status change timestamps       of the file.       After a successful call to thefopen() function, the orientation       of the stream shall be cleared, the encoding rule shall be       cleared, and the associatedmbstate_tobject shall be set to       describe an initial conversion state.       The file descriptor associated with the opened stream shall be       allocated and opened as if by a call toopen() with the following       flags:               ┌──────────────────┬───────────────────────────┐               │fopen() Modeopen() Flags│               ├──────────────────┼───────────────────────────┤               │r orrb          │ O_RDONLY                  │               │w orwb          │ O_WRONLY|O_CREAT|O_TRUNC  │               │a orab          │ O_WRONLY|O_CREAT|O_APPEND │               │r+ orrb+ orr+b │ O_RDWR                    │               │w+ orwb+ orw+b │ O_RDWR|O_CREAT|O_TRUNC    │               │a+ orab+ ora+b │ O_RDWR|O_CREAT|O_APPEND   │               └──────────────────┴───────────────────────────┘

RETURN VALUE        top

       Upon successful completion,fopen() shall return a pointer to the       object controlling the stream. Otherwise, a null pointer shall be       returned, anderrno shall be set to indicate the error.

ERRORS        top

       Thefopen() function shall fail if:EACCESSearch permission is denied on a component of the path              prefix, or the file exists and the permissions specified bymode are denied, or the file does not exist and write              permission is denied for the parent directory of the file              to be created.EINTRA signal was caught duringfopen().EISDIRThe named file is a directory andmode requires write              access.ELOOPA loop exists in symbolic links encountered during              resolution of thepath argument.EMFILEAll file descriptors available to the process are currently              open.EMFILE{STREAM_MAX} streams are currently open in the calling              process.ENAMETOOLONG              The length of a pathname exceeds {PATH_MAX}, or pathname              resolution of a symbolic link produced an intermediate              result with a length that exceeds {PATH_MAX}.ENFILEThe maximum allowable number of files is currently open in              the system.ENOENTThemode string begins with'r'and a component ofpathname              does not name an existing file, ormode begins with'w'or'a'and a component of the path prefix ofpathname does not              name an existing file, orpathname is an empty string.ENOENTorENOTDIR              Thepathname argument contains at least one non-<slash>              character and ends with one or more trailing <slash>              characters. Ifpathname without the trailing <slash>              characters would name an existing file, an[ENOENT]error              shall not occur.ENOSPCThe directory or file system that would contain the new              file cannot be expanded, the file does not exist, and the              file was to be created.ENOTDIR              A component of the path prefix names an existing file that              is neither a directory nor a symbolic link to a directory,              or thepathname argument contains at least one non-<slash>              character and ends with one or more trailing <slash>              characters and the last pathname component names an              existing file that is neither a directory nor a symbolic              link to a directory.ENXIOThe named file is a character special or block special              file, and the device associated with this special file does              not exist.EOVERFLOW              The named file is a regular file and the size of the file              cannot be represented correctly in an object of typeoff_t.EROFSThe named file resides on a read-only file system andmode              requires write access.       Thefopen() function may fail if:EINVALThe value of themode argument is not valid.ELOOPMore than {SYMLOOP_MAX} symbolic links were encountered              during resolution of thepath argument.EMFILE{FOPEN_MAX} streams are currently open in the calling              process.ENAMETOOLONG              The length of a component of a pathname is longer than              {NAME_MAX}.ENOMEMInsufficient storage space is available.ETXTBSY              The file is a pure procedure (shared text) file that is              being executed andmode requires write access.The following sections are informative.

EXAMPLES        top

Opening a File       The following example tries to open the file namedfilefor       reading. Thefopen() function returns a file pointer that is used       in subsequentfgets() andfclose() calls. If the program cannot       open the file, it just ignores it.           #include <stdio.h>           ...           FILE *fp;           ...           void rgrep(const char *file)           {           ...               if ((fp = fopen(file, "r")) == NULL)                   return;           ...           }

APPLICATION USAGE        top

       None.

RATIONALE        top

       None.

FUTURE DIRECTIONS        top

       None.

SEE ALSO        top

Section 2.5,Standard I/O Streams,creat(3p),fclose(3p),fdopen(3p),fmemopen(3p),freopen(3p),open_memstream(3p)       The Base Definitions volume of POSIX.1‐2017,stdio.h(0p)

COPYRIGHT        top

       Portions of this text are reprinted and reproduced in electronic       form from IEEE Std 1003.1-2017, Standard for Information       Technology -- Portable Operating System Interface (POSIX), The       Open Group Base Specifications Issue 7, 2018 Edition, Copyright       (C) 2018 by the Institute of Electrical and Electronics Engineers,       Inc and The Open Group.  In the event of any discrepancy between       this version and the original IEEE and The Open Group Standard,       the original IEEE and The Open Group Standard is the referee       document. The original Standard can be obtained online athttp://www.opengroup.org/unix/online.html .       Any typographical or formatting errors that appear in this page       are most likely to have been introduced during the conversion of       the source files to man page format. To report such errors, seehttps://www.kernel.org/doc/man-pages/reporting_bugs.html .IEEE/The Open Group                2017FOPEN(3P)

Pages that refer to this page:stdio.h(0p)close(3p)fclose(3p)fdopen(3p)feof(3p)ferror(3p)fgetpos(3p)fgets(3p)fgetwc(3p)fgetws(3p)fileno(3p)fmemopen(3p)fputc(3p)fputs(3p)fputwc(3p)fputws(3p)fread(3p)freopen(3p)fseek(3p)fsetpos(3p)ftell(3p)fwrite(3p)lockf(3p)open_memstream(3p)puts(3p)setbuf(3p)setvbuf(3p)stdin(3p)tempnam(3p)tmpfile(3p)tmpnam(3p)



HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface.

For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere.

Hosting byjambit GmbH.

Cover of TLPI


[8]ページ先頭

©2009-2025 Movatter.jp