Read and Write Text Files in WinCE

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Environment: Windows CE 3.0

Introduction

The CStudioFile class is intended for WinCE MFC applications because MFC for WinCE did not implement the CStdioFile text file functions. You should use the CStdioFile in Win32 programs.

This class implements both UNICODE and ASCII text file reads and writes. If the format is UNICODE, it is assumed to contain 16-bit strings. Other UNICODE formats cannot be read by WinCE programs. It is the caller’s responsibility to know which format the file is in.

Because of file buffering, programmers should not mix the CFile::Read and CFile::Write with the ReadString methods in this class. Doing so will cause unpredictable results.

CFile::typeText is stripped from the open flags when the Open method is called to avoid the ASSERT in the base class. It is not necessary to include this flag when using the CStudioFile class.

Example 1, Illustrating How to Read a Text File Using CString

  CStudioFile f;
  CString s;
  CString output;
  f.Open(L"index.html",
         CFile::modeRead | CFile::shareExclusive |
         CFile::typeText);
  while(f.ReadString(s,false))
  {
    output += s;
  }
  f.Close();

Example 2, Illustrating How to Read an ASCII-Formatted File

  CStudioFile f;
  char buf[255];
  CString output;
  f.Open(L"index.html",
         CFile::modeRead | CFile::shareExclusive |
         CFile::typeText);
  while(f.ReadString(buf, sizeof(buf))
  {
    output += buf;
  }
  f.Close();

Example 3, Illustrating How to Read an UNICODE-Formatted File

  CStudioFile f;
  wchar_t buf[255];
  CString output;
  f.Open(L"index.html",
         CFile::modeRead | CFile::shareExclusive |
         CFile::typeText);
  while(f.ReadString(buf, sizeof(buf))
  {
    output += buf;
  }
  f.Close();

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read