Home > Software engineering >  CEDITVIEW text save problem
CEDITVIEW text save problem

Time:11-02

I made a text output program CEDITVIEW,
On the screen shows the hello world
MyEditCtrl. SetSel (0, 1);
MyEditCtrl. ReplaceSel (_T (" Hello, World \ r \ n "));
After using itself with the preservation of the TXT file, with a HEX editor opens as
00000000 h: 48 00 65 00 00 6 c... ; H.E.L.L...

Why didn't the beginning of the FF FE, how to modify the program, let it keep, automatically at the beginning of the file with FF FE, because want to use excel to open this text, if there is no FF FE will quote "damaged", I need to use notepad to open, then to save, to use excel to open,

CodePudding user response:

CreateFile
WriteFile

In c/c + +

CodePudding user response:

 
//CEditStringDoc serialization
Void CEditStringDoc: : Serialize (CArchive& Ar)
{
BOOL FirstLine=TRUE;
WCHAR wBuf [2048].
Char szBuf [2048].
Int I=0;
BOOL LineEnd=FALSE;
Char type [10].
Char dummy [10].
Char * pLine=0;
Char UtfHead [3]={0 xef (char), 0 XBB (char), (char) 0 XBF};
Char UniHead [2]={0 XFF (char), (char) 0 xfe};
//
Memset (type, 0, 10);
Memset (dummy, 0, 10);
//
If (ar) IsStoring ())
{
//TODO: add storing code here
Int. All=m_TextArray GetSize ();
For (int kk=0; Kk{
Strcpy (szBuf, m_TextArray [r]. Kk GetBuffer (2048));
Strcat (szBuf, "\ r \ n");
If (FirstLine)
{
If (m_UTF8) ar. Write (UtfHead, 3);
Else if (m_UNICODE) ar. Write (UniHead, 2);
}//end of line 1 st
If (m_UTF8)
{
MultiByteToWideChar (CP_ACP, 0, szBuf, 1, wBuf, 2048);
WideCharToMultiByte (wBuf CP_UTF8, 0, 1, szBuf, 0, 2048);
Ar. Write (szBuf, strlen (szBuf));
}
Else if (m_UNICODE)
{
MultiByteToWideChar (CP_ACP, 0, szBuf, 1, wBuf, 2048);
Ar. Write (wBuf, 2 * wcslen (wBuf));
}
The else
{
Ar. Write (szBuf, strlen (szBuf));
}
FirstLine=FALSE;
}//end for
}//end save
The else
{//if the UTF8 file the 1 st 3 chars are: EFh, BBh, BFh
UNICODE file//if the 1 st 2 chars are: FEh FFh,
//TODO: add loading code here
M_TextArray. RemoveAll ();
Memset (szBuf, 0, sizeof (szBuf));
While (ar) Read (& amp; SzBuf [I], 1))
{//
If (FirstLine)
{
If ((szBuf [0]!=(char) 0 xef) & amp; &
(szBuf [0]!=(char) 0 XBB) & amp; &
(szBuf [0]!=(char) 0 XBF) & amp; &
(szBuf [0]!=(char) 0 XFF) & amp; &
(szBuf [0]!=(char) 0 xfe))
{//normal file
M_UTF8=FALSE;
M_UNICODE=FALSE;
FirstLine=FALSE;
i++;
continue;
}
If (szBuf [0]==0 (char) xef)
{
Type [0]=szBuf [0];
continue;
}
If (szBuf [0]==0 (char) XBB)
{
Type [1]=szBuf [0];
continue;
}
If (szBuf [0]==0 (char) XBF)
{
Type [2]=szBuf [0].
}
//Is UTF8 file?
If ((type [0]==0 (char) xef) & amp; &
(type [1]==0 (char) XBB) & amp; &
(type [2]==0 XBF (char)))
{//UTF8 file
M_UTF8=TRUE;
M_UNICODE=FALSE;
FirstLine=FALSE;
continue;//I=0
}
//Is UNICODE file?
If (szBuf [0]==(char) 0 XFF)
{
Type [0]=szBuf [0];
continue;
}
If (szBuf [0]==0 xfe) (char)
{
Type [1]=szBuf [0];
}
If ((type [0]==(char) 0 XFF) & amp; &
(type [1]==0 xfe (char)))
{
M_UNICODE=TRUE;
M_UTF8=FALSE;
FirstLine=FALSE;
continue;//I=0
}
}//end firstline
//check the line end
If (szBuf [I]==0 x0d)
{//
LineEnd=TRUE;
SzBuf [I]=0;
Ar. Read (dummy, 1);//00 or 0 a
If (m_UNICODE)
{
Ar. Read (dummy, 2);//0 a 00
}
}
//
If (LineEnd)
{
If (m_UTF8)
{//utf8 file
MultiByteToWideChar (CP_UTF8, 0, szBuf, 1, wBuf, 2048);
WideCharToMultiByte (wBuf CP_ACP, 0, 1, szBuf, 0, 2048);
PLine=szBuf;
}
Else if (m_UNICODE)
{//Unicode file
WideCharToMultiByte (CP_ACP, 0, (WCHAR *) szBuf, 1, (char *) wBuf, 0, 2048);
PLine=wBuf (char *);
}
The else
{//normal file
PLine=szBuf;
}
LineEnd=FALSE;//new line
//add
M_TextArray. Add (pLine);
Memset (szBuf, 0, sizeof (szBuf));
I=1;//+ 1=0 szBuf [0]
}//end LineEnd
i++;//the from 0
}//end while
//if the file end but no LF
If (szBuf [0]!=0) m_TextArray. Add (szBuf);//the last line
If (m_TextArray. GetSize ()!=0)
{
UpdateAllViews (0);
}
}
}

CodePudding user response:

See the code above, still can't understand how to deal with, I'm a beginner, can give a complete code, thank you very much!

CodePudding user response:

If (ar) IsStoring ())
{
//TODO: add storing code here
Int. All=m_TextArray GetSize ();
For (int kk=0; Kk{...

M_TextArray refers to the text displayed in the view?

CodePudding user response:

CStringArray m_TextArray;

CodePudding user response:

The class CxxxxDoc: public CDocument
{
Protected://create the from serialization only
CEditStringDoc ();
DECLARE_DYNCREATE (CEditStringDoc)

//the Attributes
Public:
CStringArray m_TextArray;
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related