2.3.63. FOPEN

The FOPEN command 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 (see fgetc).

fileid

Specifies the identity of the file to be opened. This must be a user-defined fileid. See Window and file numbers for details.

filename

Specifies the file being opened. Quotation marks are optional, but see Rules for specifying filenames 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. However, FOPEN does not create a GUI window, and output is only sent to the file. If you require the output in a window, use the VOPEN command on VOPEN.

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 ; 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

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 quotes. For example, includes/file.

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

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

    Alternatively, you can escape each backslash and use double quotes. 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

The following commands provide similar or related functionality:

The following macros provide similar or related functionality:

Copyright © 2002-2005 ARM Limited. All rights reserved.ARM DUI 0175G
Non-Confidential