fopen_s

From RAD Studio
Jump to: navigation, search

Go Up to stdio.h Index


Header File

stdio.h

Category

File access functions

Prototype

errno_t fopen_s(FILE * restrict * restrict streamptr, const char * restrict filename, const char * restrict mode);

Description

Opens a stream.

fopen_s opens the file named by filename and associates a stream to it. fopen returns a pointer used to identify the stream in subsequent operations.

The mode string used in calls to fopen is one of the following values:

Value Description

uw

Truncate the file to zero length or create text file for writing with default permissions.

ua

Append; open or create text file for writing to end-of-file with default permissions.

uwb

Truncate the file to zero length or create binary file for writing with default permissions.

uab

Append; open or create binary file for writing to end-of-file with default permissions.

uw+

Truncate the file to zero length or create text file for updating with default permission.

ua+

Append; open or create text file for updating, writing to end-of-file with default permissions.

uw+b or uwb+

Truncate the file to zero length or create binary file for updating with default permissions.

ua+b or uab+

Append; open or create binary file for updating, writing to end-of-file with default permissions.

If the operating system supports it, files opened for writing will be opened with non-shared access to avoid concurrent writes to the files. Again, if the operating system supports it, files with the mode string not starting with u will have the owner set to the user that the program is run from, and will be unavailable to other users. If the mode string starts with u, then the files will have default file access permissions.

If the file was opened successfully, the value pointing to the file is set to the streamptr parameter, otherwise streamptr is set to null.

Return Value

On successful completion, fopen_s returns zero, otherwise it returns a nonzero value.

Example

/* Program to create backup of a file */

#include <stdio.h>

int main(void)
{
  FILE *in, *out;
  in = new FILE;
  if (fopen_s(&in, "TESTFILE.DAT", "rt")){
    fprintf(stderr, "Cannot open input file.\n");
    return 1;
  }
  if (fopen_s(&out, "TESTFILE.BAK", "wt")){
    fprintf(stderr, "Cannot open output file.\n");
    return 1;
  }
  while (!feof(in)){
    fputc(fgetc(in), out);
  }
  fclose(in);
  fclose(out);
  return 0;
}

Portability

POSIX Win32 ANSI C ANSI C++

fopen_s

+

+

+

+

See Also