I have a Nokia E61i (and N95) plugged into my Windows XP machine as mass storage class (like a USB stick). Copying a 7MB file to this device using windows explorer takes about 10 seconds. Copying the same file programmatically, using C++ fstream, takes over 40 seconds.

std::ifstream sourceStream(mSourcePath, std::ios::binary);
std:fstream sinkStream (mSinkPath, std::ios::binary);

const int kBufferSize = 65536;
std::auto_ptr<char> buffer(new char[kBufferSize]);

sourceStream.read(buffer.get(), kBufferSize);

if (sourceStream.gcount() > 0)
sinkStream.write(buffer.get(), sourceStream.gcount());
while (!sourceStream.eof());


Now, if I make kBufferSize 8MB then the copy is as fast as windows explorer. If I leave the buffer size at 65K, but open the sink file in text mode, the copy takes 13 seconds (but comes out wrong due to the character substitutions). If I leave the above code untouched (65K buffer, sink file opened in binary mode), but set the removable drive to "optimize for performance" then the copy takes about 1 second. Although, how long it really takes to flush the write buffers is not known. I know it's very fast because I can "safely remove device" and pull it out a few seconds after.

Unfortunately, I can't write the file in text mode. Can anyone explain why these various circumstances give such different results? I'd like to get as near to explorer's copy performance, if possible, without requiring the user to change the "optimize for" settings in Windows.

UPDATE: If I use C# File.OpenWrite() then I get great write performance on the Nokia E61i, but no improvement on the N95. Using any other method for creating a FileStream doesn't give me the improved performance with the E61i. Someone give me a reality check. I must be doing something wrong or interpretting the results incorrectly...?