1

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.

2 Answers 2

2

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.

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

2 Comments

I think it's not the case. I have more than 2GB RAM free. File I try to create catch few hundreds MB.
I said "in case." Your program does create the file on my system, so I believe this is expected behavior.
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.