Your code should actually write (and append) single strings from the array to the file.
But you should think about how the reader can distinguish the strings. This can be done by writing a new line sequence ("\r\n" Windows style or "\n" Unix style) creating a plain text file (which might be Unicode encoded) or by appending the trailing NULL bytes:
file.Write((LPCTSTR)str, str.GetLength() * sizeof(TCHAR));
file.Write(_T("\r\n"), 2 * sizeof(TCHAR));
file.Write(str.GetString(), (str.GetLength() + 1) * sizeof(TCHAR));
But the simplest method is using serialization which is supported by
CStringArray
:
CFile file(_T("Test1.txt"), CFile::modeCreate | CFile::modeWrite);
CArchive ar(&file, CArchive::store);
RegKeys.Serialize(ar);
Which method to use is up to you but you have to use a corresponding read method.
When not using serialization you should also move the file opening and closing out of the loop:
CFile file;
if (file.Open(_T("Test1.txt"), CFile::modeWrite))
{
file.Close();
}