1
votes

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

  • The purpose of my program is to create file on RAM memory.

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.

1

1 Answers

2
votes

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.