Cfile关于打开文件的操作

1.简单的从Char到我们Cfile中需要的类型

#include <iostream>
#include <afx.h>
using namespace std ;
int main()
{
	//写
	CFile cfile;
	const char* msg="ab.txt"; 
	wchar_t *wmsg = new wchar_t[strlen(msg)+1]; //memory allocation
	mbstowcs(wmsg, msg, strlen(msg)+1);

	cfile.Open(wmsg, CFile::modeCreate | 
		CFile::modeReadWrite);
	char pbufWrite[100];
	memset(pbufWrite, 'a', sizeof(pbufWrite));
	cfile.Write(pbufWrite, 100);         
	cfile.Flush();
	delete []wmsg;
	system("pause");
}
2.不用char类型,直接用_T类型的方法

#include <iostream>
#include <afx.h>
using namespace std ;
int main()
{
	CFile cfile;
	TCHAR* pszFileName = _T("f.txt");
	cfile.Open(pszFileName, CFile::modeCreate | 
		CFile::modeReadWrite);
	char pbufWrite[100];
	memset(pbufWrite, 'a', sizeof(pbufWrite));
	cfile.Write(pbufWrite, 100);         
	cfile.Flush();
	system("pause");
}



你可能感兴趣的:(Cfile关于打开文件的操作)