Click here to Skip to main content
15,881,381 members
Please Sign up or sign in to vote.
4.33/5 (3 votes)
See more:
Hi.

I'm a complete noobe with any form of the c language (4 years in vb6) so I'm hoping someone can help me out.
I've managed to unlock my RAC Satnav which runs on wince 5 so I've downloaded embedded c++ 4. Got to start somewhere so I've tried to write something that can read a line from a text file and display it in a text box.
I've been using the following code which does appear to read the file OK but all I get in the text box is a bunch of little square characters. I suspect this is something to do with Unicode?

CFile file;
CString line;
if (file.Open(_T("\\ResidentFlash\\text.txt"), CFile::modeRead)) {
CArchive ar(&file, CArchive::load);
while (ar.ReadString(line)) {

CEdit * pEdit = (CEdit *)GetDlgItem(IDC_EDIT1);
CString strEdit = (line) + _T("test");
pEdit->SetWindowText(strEdit);

}
ar.Close();
file.Close();

The quoted "test" appears as it should but the text that I believe should be in (line) is the part that appears as little square boxes.

Can someone please offer assistance, Ive searched on Google and found several related articles but they were for c++, I cant find anything for embedded c++ and wince. c++ is not completely compatible with wince.
Posted
Updated 14-Mar-11 18:32pm
v2
Comments
pankajupadhyay29 15-Mar-11 0:33am    
edited to wrap code in <pre> block and minor spellings.

Hi,

It is very likely that your file is ANSI and not UNICODE, in that case you should read it into an ANSI string.

[Edited after OP comment]

See The Complete Guide to C++ Strings, Part I - Win32 Character Encodings[^] and part 2, for string handling tutorial.

eVC4 MFC does not support CStringA and CStringW, so you have to handle ANSI strings with the Standard C++ Library, and convert to UNICODE before calling CEdit. The following compiles OK with VS2008, hopefully will do it with eVC:
C++
#include <fstream>
#include <string>
inline void Test()
{
    std::ifstream in(_T("\\ResidentFlash\\text.txt"));
    std::string line;
    std::getline(in, line);
    CString strEdit(line.c_str());
    CEdit * pEdit = (CEdit *)GetDlgItem(IDC_EDIT1);
    pEdit->SetWindowText(strEdit + _T("test"));
}

[/Edit]


Try to copy a UNICODE file to your device, your program should read it.

cheers,
AR
 
Share this answer
 
v2
Comments
voidhawk_1 16-Mar-11 16:01pm    
Hi Alain. Thanks for your advice. You were spot on, I saved a file via notepad in the unicode format and it worked.
As mentioned I'm new to this so I wasnt aware I was reading into a unicode string, any idea how I would change my code to read ANSI ?
I got so much to learn, but thats the fun of it :)
Many thanks everyone.
Alain Rist 16-Mar-11 16:33pm    
I never used MFC on WinCe devices so I am not able to answer your question. My suggestion is to only have UNICODE text files on your device.
cheers, AR
Alain Rist 17-Mar-11 5:27am    
See my edited answer, not really for total beginner :)
Dalek Dave 17-Mar-11 7:53am    
Good Call.
voidhawk_1 20-Mar-11 18:00pm    
Hi. Many many thanks to all for the advice and info. My aim was to place a little program in the "Run" folder that could read a text file containing a list of files that would be deleted on boot up.
The files and shortcuts that I delete are replaced by the device everytime it boots, annoyingly most were on the desktop. Anyways, problem solved.
I'll try the code for converting later, I'll let you know the result.
The link to The Complete Guide to C++ strings has been VERY helpful, thanks.
C#
int FetchData(const string strFile)
{
    FILE    *       _tempFile;
    char            _buff[100];

    cout << "FetchData file name is " << strFile << endl;

    _tempFile   = fopen( strFile.c_str() , "r");

    if (_tempFile == NULL)      //file no exist
    {
        perror(" Open the data file error");
        return -1;
    }

    if ((fseek(_tempFile , 0L, SEEK_SET)) < 0)
    {
        perror("File seek error");
        fclose(_tempFile);
        return -2;
    }

    while(fgets(_buff,100,_tempFile)!=NULL)
    {
        DoSomeThingWithThisLine(_buff);    
    }
    fclose(_tempFile);

    return 1;
}
 
Share this answer
 
As far as I remember, WinCe's default character encoding is UNICODE and ANSI is not supported at all. You normally don't need to handle ANSI strings with text files created in WinCE or converted by synchronization. If you need the files transferred via transflash card or by other means, you can probably use MultiByteToWideChar(...)[^] function for ANSI to UNICODE conversion just after reading from file.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900