calling CreateFile, specifying FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE

Before I describe my problem, here is a description of the C++ program I'm writing:

I read that if specify FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE when creating file it will be loaded direct to the RAM memory.

One of blogs that talk about is this one:

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.

I have built a mini-program, but it doesn't achieve the goal. Instead, it creates a file on the hard-drive, in directory I specify.

Here's my program:

void main ()
{
   LPCWSTR str = L"c:\\temp.txt";
   HANDLE fh = CreateFile(str,GENERIC_WRITE,0,NULL,CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE,NULL);   

   if (fh == INVALID_HANDLE_VALUE)
   {
      printf ("Could not open TWO.TXT");
      return;
   }

   DWORD dwBytesWritten; 
   for (long i=0; i<20000000; i++)
   {
      WriteFile(fh, "This is a test\r\n", 16, &dwBytesWritten, NULL);
   }

   return;
}

I think there problem in CreateFile function, but I can't fix it. Please help me.

Upvotes: 1

Views: 2832

Answers (2)

apple apple
apple apple

Reputation: 10614

The file do be created, on that path you specified.

But that doesn't means windows really write the data to physical disk.

All FILE_ATTRIBUTE_TEMPORARY does is tell windows try to cache the file on RAM, other than that it would just behave like regular file.

Upvotes: 0

In silico
In silico

Reputation: 52207

Larry Osterman also mentions:

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.

So the OS creates the file in case it needs to flush the data due to memory limits.

Upvotes: 2

Related Questions