Previous Page | Next Page

SAS Component Language Dictionary

MOPEN



Opens a member file in a directory
Category: Directory

Syntax
Details
Example
See Also

Syntax

file-id=MOPEN(directory-id,member-name<open-mode <,record-length<,record-format>>>);

file-id

contains the identifier for the file, or 0 if the file could not be opened. You can use a file-id that is returned by the MOPEN function just as you would use a file-id returned by the FOPEN function.

Type: Numeric

directory-id

is the identifier that was returned by DOPEN when the directory was opened. If directory-id is invalid, the program halts.

Type: Numeric

member-name

is the name of a file in the directory that is identified by directory-id.

Type: Character

open-mode

is the type of access to the file:

'A'

APPEND mode, which allows writing new records after the current end of the file.

'I'

INPUT mode, which allows reading only. (This is the default.)

'O'

OUTPUT mode, which defaults to the OPEN mode that was specified in host-options in the FILENAME statement or function. If no host option was specified, then OUTPUT mode allows writing new records at the beginning of the file.

'S'

Sequential input mode, which is used for pipes and other sequential devices such as hardware ports.

'U'

UPDATE mode, which allows both reading and writing.

Type: Character

record-length

is the logical record length of the file. To use the existing record length for the file, specify a length of 0 or do not provide a value here.

Type: Numeric

record-format

is the record format of the file:

'B'

Interpret data as binary data.

'D'

Use the default record format.

'E'

Use an editable record format.

'F'

The file contains fixed-length records.

'P'

The file contains printer carriage-control characters in a host-dependent record format.

'V'

The file contains variable-length records.

To use the existing record format, do not specify a value here.

Type: Character


Details

CAUTION:
Use OUTPUT mode with care.

Opening an existing file for output may overwrite the current contents of the file without warning.  [cautionend]

The member file is identified by directory-id and member-name instead of by a fileref. You can also open a directory member by using FILENAME to assign a fileref to the member, followed by a call to FOPEN. However, using MOPEN saves you from having to use a separate fileref for each member.

If the file already exists and is opened with an open-mode of O, then the output mode defaults to either APPEND or REPLACE, based on host-options that were specified in the FILENAME function or statement. For example:

rc=filename('file',filename,' ','mod');
fid=fopen('file','o');
rc=fput(fid,'This is a test.');
rc=fwrite(fid);
rc=fclose(fid);

If FILE already exists, then FWRITE appends a new record. However, if no host option was specified with the FILENAME function, then FWRITE writes the value at the beginning of the file, which could replace an existing value.

If the open fails, use SYSMSG to retrieve the message text.

Operating Environment Information:   The term directory in this description refers to an aggregate grouping of files managed by the host operating system. Different host operating systems identify such groupings with different names, such as directory, subdirectory, MACLIB, or partitioned data set. See the SAS documentation for your operating environment for details.

Opening a directory member for output is not possible on some operating systems.  [cautionend]


Example

Assign the fileref MYDIR to a directory. Then open the directory, determine the number of members, retrieve the name of the first member, and open that member. The last three arguments to MOPEN are the defaults.

rc=filename('mydir','filename');
did=dopen('mydir');
frstname=' ';
memcount=dnum(did);
if (memcount>0) then
   do;
      frstname=dread(did,1);
      fid=mopen(did,frstname,'i',0,'d');
      ...SCL statements to process the member...
      rc=fclose(fid);
   end;
else
   _msg_=sysmsg();
rc=dclose(did);


See Also

DOPEN

FCLOSE

FOPEN

Previous Page | Next Page | Top of Page