12

I am using the two flags FILE_ATTRIBUTE_TEMPORARY and FILE_FLAG_DELETE_ON_CLOSE while creating temporary files in my C++ application.

According to this blog🕗 there shouldn't be any file being created on the disk:

It’s only temporary

Larry Osterman, April 19, 2004

To create a “temporary” file, you call CreateFile specifying FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE in the dwFlagsAndAttributes attribute. This combination of bits acts as a hint to the filesystem that the file data should never be flushed to disk. In other words, such a file can be created, written to, and read from without the system ever touching the disk.

But in my code the file is created and written to on disk (even for 1 KB data). Can someone confirm the exact functionality of these flags, and whether the files are created on disk or not?

2 Answers 2

13

Later on in that same link, there is the quote:

If you exceed available memory, the memory manager will flush the file data to disk. This causes a performance hit, but your operation will succeed instead of failing.

Marking a file as temporary will tell the system it doesn't need to be on disk, but it doesn't prevent it from being put there, either.

Sign up to request clarification or add additional context in comments.

Comments

9

It just says that the file will never be flushed to disk. That means, while it exists in your filesystem, it will never be physically stored on your hard drive. The file system will show it though, with actual size and all.

4 Comments

but then is there a way to not have temporary files being created on disk at all...
@SVJ: The point is that the file will only be written to disk if there is not enough memory. In that case, what would you have the program do? Why are you using temporary files in the first place? Maybe there are alternatives.
My application uses temp files to store and manipulate data for some time and then dumps it back to the original input files. The problem is that beacuse of this IO the anti-virus slows down the whole processing. So i need a scenario where ther's no IO to the disk at all. The flags have helped, but i still see the files on disk and their sizes around 5-30kb..Can this be avoided?
@SVJ: The anti-virus will likely check anything that goes to the filesystem (that is not the same as your disk). In that case, just use a buffer in your program to store the data.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.