PreviousNext

Reading and Writing Files

The dcecp commands for reading and writing to files look and act like their C language counterparts - fopen, fclose, and so on.

Open a file for reading and writing using the open command. The second argument to the open command (shown in the following example as +r) specifies the file access mode. You can open files for reading, or writing, or both and you can specify whether to replace existing files or to add to them with new information. You can also set the initial access position to the beginning or the end of a file. The default access mode is read only (the file must already exist).

dcecp> open server_snap.dcecp +r
file5
dcecp>

The open command assigns a file identifier to each file when it is opened. Use the file identifier to refer to files in subsequent commands.

Once a file is opened, you can add lines to a file using the puts command. Normally, dcecp waits until it has accumulated sufficient data before writing this information to a file. If you want dcecp to immediately write the information to a file, use the flush command. Use gets to read the next line from a file or use read to read a number of bytes or all of the bytes in a file. The following example writes a list of all principals in a file named prins:

dcecp> open prins w+
file8
dcecp> puts file8 [principal catalog]
dcecp> close file8
dcecp>

Sometimes, you do not want to start reading or writing at the first line of a file. The DCE control program provides several commands that set the access position so you do not have to advance through every line in the file. These commands will produce an error if you use them for devices like terminals or other sequential devices that don't support random access. Use the seek command to set the access point in a file. Specify the offset as a number of bytes from the origin which can be the beginning or end of the file or the current position. Use a negative number to move toward the beginning of the file as in the following example which moves back sixteen bytes from the current access position.

dcecp> seek file5 -16 current
dcecp>

You can determine the current access position using the tell command. Save the return value in a variable so you can go back to that position in the file later on.

Finally, you can close a file using the close command.

dcecp> close file5
dcecp>