Admin Production ni-theme
Current Publication

CreateAndOpenTemporaryFile

LabWindows/CVI

CreateAndOpenTemporaryFile

int CreateAndOpenTemporaryFile (const char pathname[], const char tempFilenamePrefix[], const char tempFilenameExtension[], const char openModeString[], char tempFilePathname[], FILE **tempFileHandle);

Purpose

Creates a temporary file in the same directory as the file identified by pathname. This function is intended to be used when writing out a new version of the file identified by pathname. By writing out a temporary file in the same directory as the target file, you can make sure that there is enough room on the disk for the new file. Otherwise, you might destroy the old contents of the file without being able to write the new version of the file. When you have successfully written the temporary file, you can use the DeleteAndRename function to delete the old version of the target file and then rename the temporary file to the target file.

The function operates as follows:

It generates temporary file names using:

  • the directory of the pathname
  • the Temp Filename Prefix (which should be 5 characters in length)
  • followed by an index of up to three characters (e.g. "1", "20", "101")
  • followed by a dot
  • followed by the tempFilenameExtension (which should be from 1 to 3 characters in length and should not contain a dot)

For each temporary file name, the function checks to see if the file already exists. If the file does not already exist, the function attempts to open the file for writing and returns success or failure based on whether the open call succeeded.

If files exist for each of the 1000 temporary file names that can be generated, an error is returned.

If the function succeeds, the pathname of the temporary file is returned in tempFilePathname and the file handle is returned in tempFileHandle.

Parameters

Input
Name Type Description
pathname const char[] The pathname of a file. If it is not an absolute pathname, the pathname is relative to the current working directory.

Only the drive and directory portions of the pathname are used in creating the temporary file. The file name portion of this parameter is not used.
tempFilenamePrefix const char[] A string containing the characters to be used in generating the first portion of the temporary file names. There should be from one to five characters only.
tempFilenameExtension const char[] The extension to be used in the temporary file names. It should contain from one to three characters, and it should not contain a dot.
openModeString const char[] The open mode string to be passed to the fopen call within this function. Refer to the documentation for fopen in the ANSI C Library.
Output
Name Type Description
tempFilePathname char [] If this function succeeds in creating a temporary file, its pathname is returned in this buffer.
tempFileHandle FILE * Returns the file pointer (type FILE *) for the temporary file.

Return Value

Name Type Description
status int Indicates if the temporary file was created.

If the function was successful, it returns 0. Otherwise, it returns a negative error code. The error codes that can occur and the header files in which they are defined are listed below:

–91 Too many files are open. (userint.h)
–93 Input/Output error. (userint.h)
–5003 Could not generate an unused temporary file name in the same directory as the output file. (toolbox.h)
–5004 Could not create a temporary file in the same directory as the output file. (toolbox.h)

Additional Information

Library: Programmer's Toolbox

Include file: toolbox\toolbox.h

LabWindows/CVI compatibility: LabWindows/CVI 4.0 and later