Read and Write Text Files in WinCE

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();


Downloads

Comments

  • Exception....help me....

    Posted by Legacy on 12/26/2003 12:00am

    Originally posted by: Flora

    my code is .....
    
    

    char * filename = "c:\\test.txt";

    CFileException *pError = NULL;
    CStdioFile fileLog;
    CString s;
    CString output;
    fileLog.Open((unsigned short *)filename, CFile::modeRead | CFile::shareExclusive |
    CFile::typeText);
    while (fileLog.ReadString(s,false))
    {
    output += s;
    }

    first question compiler tell me
    rror C2664: 'unsigned short *__thiscall CStdioFile::ReadString(unsigned short *,unsigned int)' : cannot convert parameter 1 from 'class CString' to 'unsigned short *'

    so i change the code

    char * filename = "c:\\test.txt";

    CFileException *pError = NULL;
    CStdioFile fileLog;
    unsigned short * s;
    CString output;
    fileLog.Open((unsigned short *)filename, CFile::modeRead | CFile::shareExclusive |
    CFile::typeText);
    while (fileLog.ReadString(s,false))
    {
    output += s;
    }

    but i have a exception
    how can i do

    Reply
  • Nice to add "\r\n"

    Posted by Legacy on 10/29/2003 12:00am

    Originally posted by: kristian jerpetj�n

    Added line

    content += "\r\n"

    in example 1.
    This gave a nicer text editor mode ;)

    Reply
  • CStudioFile ?

    Posted by Legacy on 07/29/2003 12:00am

    Originally posted by: flaffy

    CStudioFile ???

    And CStudioFile doesnt exist - only CStdFile - and there ReadString isnt available?

    Reply
  • What these sentences mean?

    Posted by Legacy on 10/18/2002 12:00am

    Originally posted by: dozoo

    if(m_ibuf[m_nextout] == '\n' && (m_nextout < _MAX_BUFSIZE))
    {
    m_nextout++;
    }

    Reply
  • Why not use fopen _wfopen etc

    Posted by Legacy on 09/16/2002 12:00am

    Originally posted by: Hans Wedemeyer

    fopen and _wfopen are supported under WinCE 3.0 (I'll check 4.0) and are easier to use....

    Reply
  • Couldn't get WriteString to work

    Posted by Legacy on 09/10/2002 12:00am

    Originally posted by: Pat R

    Was WriteString tested?

    Is there an example? Does it just not work on the emulator?

    Reply
  • Very good, another way....

    Posted by Legacy on 09/06/2002 12:00am

    Originally posted by: Heng cao

    I use CArchive to read text file in WinCE 3.0 (eVC++3.0)
    e.g

    CFile file( lpszFileName, CFile::modeRead );
    CArchive ar( &file, CArchive::load );

    char buf[2];
    ar.Read( buf, 2 );//ignore first two bytes of unicode file signature
    CString line;
    while( ar.ReadString( line ) )
    {
    ....
    }

    Reply
  • To ASSERT or not to ASSERT ...

    Posted by Legacy on 09/03/2002 12:00am

    Originally posted by: Gregory J. Spiers

    Your main text states "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."

    Your three examples all use this as an open flag; Does this not cause an ASSERT after all?

    Reply
  • Typing mistakes...

    Posted by Legacy on 08/05/2002 12:00am

    Originally posted by: Helmut

    Maybe in a rush you made following mistake:
    -In example 2 and 3 there is no "s". Better to take buf and for unicode's sake the TEXT macro.

    Quote:
    There is nothing better than a living community!

    Reply
  • Good.....

    Posted by Legacy on 08/02/2002 12:00am

    Originally posted by: Red

    Thank You
    I Use This Lib.....

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Bonita BPM 7 is here! And for you, an in-depth tour of its innovation. In this webinar, we'll guide you through the brand-new features of this release and demonstrate them live. You will: Discover how business application implementation and maintenance is now even easier, with decoupling of process logic, business data and user interface Learn how to create "living applications" for business processes which present business data in custom views We recommend that you watch the half-hour recording of our …

  • What does it take to win? According to Jack Welch, winning in business is great because when companies win, people thrive and grow. However, it goes without saying that you have to win the right way -- cleanly and by the rules. Even the most talented businessperson with the best intentions will get nowhere unless he or she knows how to win in today's complex business world. Read this book summary to learn not only the strategies of winning, but also the value that those strategies bring to your professional …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date