CFile file; if(file.Open(_T("test.txt"), CFile::modeRead)) { TCHAR str[100]; while(file.ReadString(str, 100)) { _tprintf(_T("%s"), str); } file.Close(); }
CFile file; if(file.Open(_T("test.txt"), CFile::modeCreate | CFile::modeWrite)) { TCHAR str[] = _T("This is a test message."); file.Write(str, sizeof(str)); file.Close(); }This example demonstrates how to write data to a file using the Write() function of CFile class. A message is created in a character array and then written to a file named "test.txt". The CFile class is a part of the MFC (Microsoft Foundation Classes) library which provides a rich set of classes for Windows programming.