2.3.67. FOPEN

Opens a file and assigns to it a specified file number.

Syntax

FOPEN [/A] [/R] fileid ,filename

where:

/A

Appends new data to an existing file. You cannot read or write the existing information, and the existing information is retained.

/R

Opens a file as read-only. You must use this qualifier if you want to read the file with the fgetc() macro.

fileid

Specifies the identity of the file to be opened. This must be a user-defined fileid.

filename

Specifies the file being opened. Quotation marks are optional, but see Rules for specifying filenames in the FOPEN command for details on how to specify filenames that include a path.

Description

This command enables you to read or write a file on the host filesystem by associating it with a RealView Debugger custom file number.

The file is opened for writes only by default, but you can specify append or read-only modes instead. You write to the file using the FPRINTF command, the fputc or fwrite macros, or by redirecting output from those commands that accept the fileid specifier. You read the file using either the fgetc or fread macros. You close the file using the VCLOSE command.

Note

Be aware of the following:

  • The FOPEN command runs asynchronously unless it is used in a macro.

  • If you open a new or existing file for writing, no data is written to the file until the output buffer is flushed. This happens when you close the file with the VCLOSE command, but it might happen at other times because the buffered I/O behavior is the same as in C.

  • If you specify a filename with a path that does not exist, then an error message is displayed. RealView Debugger does not create the non-existent path.

Rules for specifying filenames in the FOPEN command

Follow these rules when specifying a filename:

  • If the filename consists of only alphanumeric characters, slashes, or a period, but the filename does not start with a slash, then you do not have to use quotation marks. For example, includes/file.

  • Filenames with a leading slash must be in double quotation marks, for example "/file".

  • Filenames containing a backslash must be in single quotation marks. For example '\file' or 'c:\myfiles\file'.

    Alternatively, you can escape each backslash and use double quotation marks. For example, "c:\\myfiles\\file".

  • You can use environment variables to specify paths to a file. For example, if PATHROOT=C:\MYFILES and PATHTEST=TEST1:

    '$PATHROOT\$PATHTEST\test1.c'
    

    You can include:

    • the filename as part of the second environment variable, and then specify '$PATHROOT\$PATHTEST'.

    • the path separator in the environment variable, and then specify '$PATHROOT$PATHTEST'.

Examples

The following examples show how to use FOPEN:

fopen 50, 'c:\temp\file.txt'
fprintf 50, "Start of function\n"

Open a file and write some text to it.

fopen /r 50, 'c:\temp\file.txt'
ce fgetc(50)

Open a file and read the first character of the file.

See also

Copyright © 2002-2011 ARM. All rights reserved.ARM DUI 0175N
Non-ConfidentialID052111