Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Microsoft C - <b>fopen() open a file</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
fopen()                  Open a File

 #include   <stdio.h>

 FILE       *fopen(pathname,type);
 char       *pathname;                   Path name of file
 char       *type;                       Type of access permitted

    fopen() opens the file specified by 'pathname'.  'type' is a
    character string that specifies the type of access requested for the
    file:

        "r"     Open for reading only.  The file must exist.

        "w"     Create a new file for writing, or open an existing file
                for writing.  Use with care: If the file already exists,
                its contents will be destroyed.

        "a"     Append.  Open for writing at the end-of-file, or create
                for writing if the file doesn't exist.  Existing data
                cannot be overwritten in this mode.

        "r+"    Update. Open an existing file for reading and writing;
                the file must exist.

        "w+"    Open an empty file for reading and writing. Use with
                care: if the file exists, its contents will be destroyed.

        "a+"    Open for reading and appending; create the file if it
                doesn't exist.  All write operations take place at the
                end of the file; existing data cannot be overwritten.

    The following characters may be appended to the above 'types' to
    specify the translation mode for the new lines:

        t       Open in text (translated) mode; The CR-LF pair is
                translated to a single LF on input; LF is translated to
                CR-LF on output.

        b       Open in binary (untranslated) mode.  The above
                translation is suppressed.

    If 't' or 'b' is not specified, the translation mode is defined by
    the default mode variable '_fmode'.  '_fmode' can be set to O_BINARY,
    binary mode, or O_TEXT, text mode.  These constants are defined in
    <fcntl.h>.

    Returns:    A pointer to the open file.  NULL is returned on error.

      Notes:    Both reading and writing are allowed when using "r+",
                "w+", or "a+".

                Before switching between reading and writing, there must
                be an intervening fseek() or rewind().

  -------------------------------- Example ---------------------------------

    The following statements open and close a file.

          #include <stdio.h>

          FILE *in;

          main()
          {
              if ((in = fopen("new.dat","w+")) != NULL) {
                  .
                  .
                  .
                  fclose(in);
              }
          }

See Also: fdopen() freopen() open() fclose() fcloseall()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson