XEIM 用的 CFile 读写CString

来源:互联网 发布:php写文件换行 编辑:程序博客网 时间:2024/05/16 11:46

CFile 读写CString 是一件非常简单的事,下面是源码,希望有用。喜欢的下次还来访问 蚊子132

(1)  写入

CFile W;
W.Open(_T("Test.txt"), CFile::modeCreate | CFile::modeWrite);  CString s1 = _T("You are a Pig");
int size = s1.GetLength();
W.Write(&size, sizeof(int));
W.Write(s1, 2*s1.GetLength());  // 必须2倍,因为宽字符,另外 s1 不能写成 &s1
W.Close();


(2) 读取, 需要用BUFFER, 在读取过程中, 没有出现2倍的情况,很统一,包括不需要用TCHAR[size + 1]

CFile R;
R.Open(_T("Test.txt"), CFile::modeRead);
int size;
R.Read(&size, sizeof(int));
TCHAR * buf = new TCHAR[size];
wmemset(buf, 0, size);   // 注意是WMEMSET,而不是MEMSET
R.Read(buf, size);

CString s1 = (CString)buf;
delete []buf;

R.Close();


读取的另一种方法
CString sR
R.Read(sR.GetBuffer(), size);
但是结束时候出了问题。目前还不知道为什么。

原文:XEIM 用的 CFile 读写CString


0 0
原创粉丝点击