- Description of how Word creates temporary files
- Summary
- More Information
- Definition of a temporary file
- Why does Word create temporary files?
- Speed
- Data Integrity
- Types of temporary files
- MS-DOS-Based File
- Document-File-Based File
- Transacted Document Files
- Direct
- Specific files that Word creates
- A simplified view of the scheme used to save an edited file
- When and where Word creates temporary files
- Embedded Word Objects (Temp Directory)
- Scratch File (Temp Directory)
- Recorded Macro (Temp Directory)
- Converted Files (Temp Directory)
- Locked Files (Temp Directory)
- Saved Files (Same Directory as the Saved File)
- Owner File (Same Directory as Source File)
- Automatic Save
- Word Auto Recover Save Directory
- The location of temporary files when you close a file
- Create temporary file in C, MS Windows system
- tmpfile
- Description
- Return Values
- Examples
- See Also
- User Contributed Notes 7 notes
Description of how Word creates temporary files
For a Microsoft Word 97 version of this article, see 89247.
Summary
This article explains when, where, and how Microsoft Word for Windows creates temporary files.
More Information
Definition of a temporary file
A temporary file is a file that is created to temporarily store information in order to free memory for other purposes, or to act as a safety net to prevent data loss when a program performs certain functions. For example, Word determines automatically where and when it needs to create temporary files. The temporary files only exist during the current session of Word. When Word is shut down in a normal fashion, all temporary files are first closed and then deleted.
Why does Word create temporary files?
Speed
If there is not enough memory available to keep the document with all its edits in memory and still perform manipulations such as sorting, dragging, scrolling, and so on quickly along with any other applications that are running, then Word moves part of its code that is not being used or part of the document that is not being edited from memory to disk. This movement to temporary files on disk frees more memory for text manipulation or storage of the parts of the document that are being actively edited.
Data Integrity
Word uses temporary files as a «safety net» to protect against system errors in its file-saving scheme. By saving to a temporary file first and then renaming the file to the proper name, Word ensures the data integrity of your original file against problems (such as a power failure or lost network connections) that may occur while the file is being written.
Types of temporary files
MS-DOS-Based File
These are standard MS-DOS files.
Document-File-Based File
The difference between this file and a traditional MS-DOS file is that multiple programs can read and write to these files without the original owner knowing about it. Additionally, document files have inherent properties that allow Word to create files and directories within files. At startup, Word creates one temporary (direct) document file called
wrfxxxx.tmp. You can determine that it is a document file because the initial size is 1,536 bytes. This temporary document file is used to store all OLE objects that belong to unnamed documents, Undo, the Clipboard, and documents whose native formats are not document format (for example, .txt, .rtf, and Word 2.0 documents). Word can open document files using two different modes: transacted and direct. These modes are discussed later in this article.
Transacted Document Files
Transacted files allow Word to open a file, write to it, and have other programs—such as Microsoft Excel—write to it, but still retain the right to restore the file to the state it was in when Word first opened it.
To do this, the document file creates ghost images (typically
dftxxxx.tmp) of all the changes made to the file after it was opened; if Word keeps all the changes, the contents of
dftxxxx.tmp merge with the original file, and then saves a complete version of it. Conversely, if Word discards all changes, then
dftxxxx.tmp is deleted, and the original file does not change. Word opens all of the Word native files using transacted files, which create ghost images in the Temp directory. When you start Word, Normal.dot is typically opened in transacted mode, and a ghost file is created for it called dftxxxx.tmp. FastSave, for example, merges these two files when a save occurs.
Direct
Word uses direct storage when opening the temporary document file and when performing either a Save As or a Full Save (non-FastSave save). This type of file is a low (if any) consumer of memory and does not create a ghost image when created or opened.
Specific files that Word creates
The following tables list some of the specific temporary files that Word creates.
Files typically created when Word is started File name
————————————————————————
MS-DOS-based file (to reserve 4 file handles) 0 bytes
wrf0000.tmp
MS-DOS-based scratch file 0 bytes
mfxxxx.tmp
Compound file — transacted 0 bytes
dftxxxx.tmp
Compound file — direct 1536 bytes
wrf0001.tmp
(unnamed non-Word/OLE files)
wraxxxx.tmp
AutoRecovery AutoRecovery save of .asd
wrcxxxx.tmp
Word document
wrdxxxx.tmp
Temp document file
wrmxxxx.tmp
Word OLE document
wroxxxx.tmp
Scratch file
wrsxxxx.tmp
Converted (foreign) document
A simplified view of the scheme used to save an edited file
Word gains significant performance speed by placing the temporary file in the same directory as the saved file. If Word placed the temporary file elsewhere, it would have to use the MS-DOS COPY command to move the temporary file from the other directory to the saved location. By leaving the temporary file in the same directory as the saved document file, Word can use the MS-DOS MOVE command to quickly designate the temporary file as the saved document.
When and where Word creates temporary files
The location where Word creates the temporary files is hardcoded information and cannot be edited. Therefore, it is important that NTFS permissions for the user are set accordingly. For more information, click the following article number to view the article in the Microsoft Knowledge Base:
277867 Windows NTFS permissions are required when you run Word on any NTFS partition that has Windows 2000, Windows XP Professional, Windows Server 2003, or Windows Vista installed
In general, Word creates temporary files for the following types of data.
Embedded Word Objects (Temp Directory)
When Word acts as an OLE server program, the embedded Word objects are stored as temporary files in the Temp directory.
OLE 2.0 requires extra drive storage. When you start OLE programs, Word needs to provide copies of the data to the server. It is not unusual for extensive OLE 2.0 usage in a single session of a program to accumulate a large amount of temporary storage on the hard drive.
Scratch File (Temp Directory)
When Word runs out of internal random access memory (RAM), it always creates a single temporary scratch file in the Temp directory to hold information. This scratch file holds information that is swapped out from the Word internal file cache, which is allocated from global system memory. The scratch file varies in size from 64 kilobytes (KB) to 3.5 megabytes (MB).
The default cache size in Word is 64 KB.
For more information about how to increase the cache size in Word, click the following article number to view the article in the Microsoft Knowledge Base:
212242 Where settings are stored in the registry for Word 2000
Recorded Macro (Temp Directory)
When you record a macro, Word creates a temporary file in the Windows Temp directory.
Converted Files (Temp Directory)
The word processor converters supplied with Word create temporary files in Rich Text Format (RTF), which Word uses to access specific converters.
Locked Files (Temp Directory)
When you open a file that is locked, either because it is open in another window of Word or because another user on the network has it open, you can work with a copy of the file. Word places this copy in the Windows Temp directory. Likewise, if a template attached to a document is locked, Word automatically makes a copy of the template in the Temp directory. The copy of a locked file does not automatically update the original owner’s file.
Saved Files (Same Directory as the Saved File)
When you click Save on the File menu, the following happens:
Word builds a new temporary file using the edited version of the document.
After Word creates the temporary file, Word deletes the previous version of the document.
Word renames the temporary file to the same name as the previous version of the document.
Text Pasted Between Files (Same Directory as Source File):
When Word copies and pastes between documents, it may create a temporary file in the same directory as the source file. This is especially true if the source file is saved or closed. The temporary file represents the information that was referenced by the Clipboard prior to saving the file. Word creates this temporary file by renaming the old copy of the file to a temporary file name.
Owner File (Same Directory as Source File)
When a previously saved file is opened for editing, for printing, or for review, Word creates a temporary file that has a .doc file name extension. This file name extension begins with a tilde (
) that is followed by a dollar sign ($) that is followed by the remainder of the original file name. This temporary file holds the logon name of person who opens the file. This temporary file is called the «owner file.»
When you try to open a file that is available on a network and that is already opened by someone else, this file supplies the user name for the following error message:
This file is already opened by user name. Would you like to make a copy of this file for your use?
Word may be unable to create an owner file. For example, Word cannot create an owner file when the document is on a read-only share. In this case, the error message changes to the following error message:
This file is already opened by another user. Would you like to make a copy of this file for your use?
Note Word automatically deletes this temporary file from memory when the original file is closed.
When you open a file by using the HTTP or FTP protocol, the file is first copied to the temp directory. Then, the file is opened from the temp directory.
When you open a file on a UNC share with Word 2007, the file is first copied to the temp directory. Then, the file is opened from the temp directory.
Automatic Save
Word Auto Recover Save Directory
The temporary file that is created when Word performs an automatic save is stored in the Temp folder, unless there is not a valid Temp folder. In this case, Word saves the temporary file in the same folder where it saves the document.
The location of temporary files when you close a file
Word may occasionally have to maintain a link to a file after it is closed. This occurs when text has been copied to the Clipboard from the file. When you close a file, Word attempts the following actions:
If the selection that was copied to the Clipboard does not contain multiple sections or a picture, or is not large, Word copies the piece of the document to the scratch file.
If the copied selection does contain pictures or multiple sections, or if the file is on a floppy disk, Word copies the entire file to the Temp directory and moves the pointer there.
Create temporary file in C, MS Windows system
Basically, i have a program that is given a 4 meg compressed file, it has to decode this file into uncompressed
100 meg, then compress it back into
4 meg file. I need to store this intermediate 100 meg file somewhere on the drive (dont want to keep it in memory).
Program is written in C and will be executed on MS Windows 7. At the moment of uncompressing, no guaranteed folder (with write access) is given to the program (folder with source file might be read only and folder with target file might be not yet specified).
This has proven to be not an easy task:
1) I have read about a C function that creates a temp file that will disappear when closed or program is terminated. However, from what i understand it tries to make the file on disk C, in root directory, so this will obviously fail if user has no rights for that (which normal user doesnt)
2) I had an idea to use environmental/system variable TEMP and create a file there, BUT looking on a random Win7 PC which wasnt tweaked, i see that this variable points to c:/windows/temp, and that folder has specific rights for «users» — that is, they have rights to read, execute, create and write files, but not to delete them, check their attributes, etc. This means, i assume, that if program is ran with user privilleges, it will be able to make a file but not able to delete it, so the only way to «delete» it would be to open the file for writing and then close it, making it a 0 length file. This is also not desired, and i dont know how to query for system variables from C
3) So, basically, only idea i have right now is to make a function to open file that:
- tries to create a temp file in the output dir, if possible
- if failed, tries to create a temp file in input dir
- if failed, tries to create a temp file in TEMP dir from system variable
- if failed, tries to create a temp file in TMP dir from system variable
and a delete function that:
- tries to remove() the file (by its name that is stored somewhere)
- if failed, it tries to open the file for write, and close it, so it becomes a 0 byte file
Are there better ideas?
Any help is appreciated, thanks!
PS: Program must not use any external libraries like MFC or something, only built-in standart C functions
tmpfile
(PHP 4, PHP 5, PHP 7, PHP 8)
tmpfile — Creates a temporary file
Description
Creates a temporary file with a unique name in read-write (w+) mode and returns a file handle.
The file is automatically removed when closed (for example, by calling fclose() , or when there are no remaining references to the file handle returned by tmpfile() ), or when the script ends.
If the script terminates unexpectedly, the temporary file may not be deleted.
Return Values
Returns a file handle, similar to the one returned by fopen() , for the new file or false on failure.
Examples
Example #1 tmpfile() example
The above example will output:
See Also
- tempnam() — Create file with unique file name
- sys_get_temp_dir() — Returns directory path used for temporary files
User Contributed Notes 7 notes
To get the underlying file path of a tmpfile file pointer:
= tmpfile ();
$path = stream_get_meta_data ( $file )[ ‘uri’ ]; // eg: /tmp/phpFx0513a
I found this function useful when uploading a file through FTP. One of the files I was uploading was input from a textarea on the previous page, so really there was no «file» to upload, this solved the problem nicely:
# Upload setup.inc
$fSetup = tmpfile ();
fwrite ( $fSetup , $setup );
fseek ( $fSetup , 0 );
if (! ftp_fput ( $ftp , «inc/setup.inc» , $fSetup , FTP_ASCII )) <
echo «
Setup file NOT inserted
» ;
>
fclose ( $fSetup );
?>
The $setup variable is the contents of the textarea.
And I’m not sure if you need the fseek($temp,0); in there either, just leave it unless you know it doesn’t effect it.
Since this function may not be working in some environments, here is a simple workaround:
function temporaryFile($name, $content)
<
$file = DIRECTORY_SEPARATOR .
trim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) .
DIRECTORY_SEPARATOR .
ltrim($name, DIRECTORY_SEPARATOR);
register_shutdown_function(function() use($file) <
unlink($file);
>);
at least on Windows 10 with php 7.3.7, and Debian Linux with php 7.4.2,
the mode is not (as the documentation states) ‘w+’ , it is ‘w+b’
(an important distinction when working on Windows systems)
No, the fseek() is necessary — after writing to the file, the file pointer (I’ll use «file pointer» to refer to the current position in the file, the thing you change with fseek()) is at the end of the file, and reading at the end of the file gives you EOF right away, which manifests itself as an empty upload.
Where you might be getting confused is in some systems’ requirement that one seek or flush between reading and writing the same file. fflush() satisfies that prerequisite, but it doesn’t do anything about the file pointer, and in this case the file pointer needs moving.