概述
检查字符集是否Unicode.
再参考示范代码:
void CCFile_testDlg::OnBnClickedBtnFwrite()
{
// TODO: 在此添加控件通知处理程序代码
char szCurrentDateTime[32];
char szCurrentDateTime1[32];
char path1[32] = {"d:\txw\"};
CString path2,path5;
CTime nowtime;
nowtime = CTime::GetCurrentTime();
sprintf_s(szCurrentDateTime, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d",
nowtime.GetYear(), nowtime.GetMonth(), nowtime.GetDay(),
nowtime.GetHour(), nowtime.GetMinute(), nowtime.GetSecond());
sprintf_s(szCurrentDateTime1, "%.4d-%.2d-%.2d",
nowtime.GetYear(), nowtime.GetMonth(), nowtime.GetDay());
//path2.Format(_T("%s"), szCurrentDateTime1);
strcat_s(path1, szCurrentDateTime1);
strcat_s(path1, ".txt");
path2.Format(_T("%s"), path1);
CString douhao = _T(",");
CString hanzi = _T("自然");
CString strEnter = _T("rn");//换行
path5 = szCurrentDateTime; //日期时间
//AfxMessageBox(path2);
CFile file_1;
file_1.Open(_T("d://txw//tt19.csv"), CFile::modeNoTruncate | CFile::modeCreate | CFile::modeWrite);//打开文件
file_1.SeekToEnd();//数据位置跳到文件末端
//WORD unicode = 0xFEFF; //这句重要
wchar_t unicode = 0xFEFF; //添加这个文件头,记事本打开才能识别里面的汉字,不会乱码。
file_1.Write(&unicode, 2);
file_1.Write(path5, wcslen(path5) * sizeof(wchar_t));//日期
file_1.Write(douhao, wcslen(douhao) * sizeof(wchar_t));//逗号
file_1.Write(edit_wwid, wcslen(edit_wwid) * sizeof(wchar_t));//工号
file_1.Write(douhao, wcslen(douhao) * sizeof(wchar_t));//逗号
file_1.Write(hanzi, wcslen(hanzi) * sizeof(wchar_t));//汉字
file_1.Write(douhao, wcslen(douhao) * sizeof(wchar_t));
file_1.Write(edit_name, wcslen(edit_name) * sizeof(wchar_t));//英文名字
file_1.Write(douhao, wcslen(douhao) * sizeof(wchar_t));
file_1.Write(edit_sex, wcslen(edit_sex) * sizeof(wchar_t));//性别
file_1.Write(strEnter, wcslen(strEnter) * sizeof(wchar_t)); //换行
file_1.Close();
MessageBox(L"数据发送成功",L"cfile 提示");
}
完整代码参考:
// CFile_testDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "CFile_test.h"
#include "CFile_testDlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_ABOUTBOX };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// CCFile_testDlg 对话框
CCFile_testDlg::CCFile_testDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(IDD_CFILE_TEST_DIALOG, pParent)
, edit_wwid(_T(""))
, edit_name(_T(""))
, edit_sex(_T(""))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCFile_testDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
// DDX_Control(pDX, IDC_EDIT1_wwid, edit_wwid);
// DDX_Control(pDX, IDC_EDIT2_name, edit_name);
// DDX_Control(pDX, IDC_EDIT3_sex, edit_sex);
DDX_Text(pDX, IDC_EDIT1_wwid, edit_wwid);
DDX_Text(pDX, IDC_EDIT2_name, edit_name);
DDX_Text(pDX, IDC_EDIT3_sex, edit_sex);
}
BEGIN_MESSAGE_MAP(CCFile_testDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_Btn_Fwrite, &CCFile_testDlg::OnBnClickedBtnFwrite)
ON_BN_CLICKED(IDC_BUTTON2, &CCFile_testDlg::OnBnClickedButton2)
END_MESSAGE_MAP()
// CCFile_testDlg 消息处理程序
BOOL CCFile_testDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
edit_wwid =_T("10699487");
edit_name =L"txw";
edit_sex =L"male";
SetDlgItemText(IDC_EDIT1_wwid, edit_wwid);
SetDlgItemText(IDC_EDIT2_name, edit_name);
SetDlgItemText(IDC_EDIT3_sex, edit_sex);
//edit_name.SetWindowText(_T("stxw"));
//edit_sex.SetWindowText(_T("Male"));
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void CCFile_testDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CCFile_testDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CCFile_testDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CCFile_testDlg::OnBnClickedBtnFwrite()
{
// TODO: 在此添加控件通知处理程序代码
char szCurrentDateTime[32];
char szCurrentDateTime1[32];
char path1[32] = {"d:\txw\"};
CString path2,path5;
CTime nowtime;
nowtime = CTime::GetCurrentTime();
sprintf_s(szCurrentDateTime, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d",
nowtime.GetYear(), nowtime.GetMonth(), nowtime.GetDay(),
nowtime.GetHour(), nowtime.GetMinute(), nowtime.GetSecond());
sprintf_s(szCurrentDateTime1, "%.4d-%.2d-%.2d",
nowtime.GetYear(), nowtime.GetMonth(), nowtime.GetDay());
//path2.Format(_T("%s"), szCurrentDateTime1);
strcat_s(path1, szCurrentDateTime1);
strcat_s(path1, ".txt");
path2.Format(_T("%s"), path1);
CString douhao = _T(",");
CString hanzi = _T("自然");
CString strEnter = _T("rn");//换行
path5 = szCurrentDateTime; //日期时间
//AfxMessageBox(path2);
CFile file_1;
file_1.Open(_T("d://txw//tt19.csv"), CFile::modeNoTruncate | CFile::modeCreate | CFile::modeWrite);//打开文件
file_1.SeekToEnd();//数据位置跳到文件末端
//WORD unicode = 0xFEFF; //这句重要
wchar_t unicode = 0xFEFF; //添加这个文件头,记事本打开才能识别里面的汉字,不会乱码。
file_1.Write(&unicode, 2);
file_1.Write(path5, wcslen(path5) * sizeof(wchar_t));//日期
file_1.Write(douhao, wcslen(douhao) * sizeof(wchar_t));//逗号
file_1.Write(edit_wwid, wcslen(edit_wwid) * sizeof(wchar_t));//工号
file_1.Write(douhao, wcslen(douhao) * sizeof(wchar_t));//逗号
file_1.Write(hanzi, wcslen(hanzi) * sizeof(wchar_t));//汉字
file_1.Write(douhao, wcslen(douhao) * sizeof(wchar_t));
file_1.Write(edit_name, wcslen(edit_name) * sizeof(wchar_t));//英文名字
file_1.Write(douhao, wcslen(douhao) * sizeof(wchar_t));
file_1.Write(edit_sex, wcslen(edit_sex) * sizeof(wchar_t));//性别
file_1.Write(strEnter, wcslen(strEnter) * sizeof(wchar_t)); //换行
file_1.Close();
MessageBox(L"数据发送成功",L"cfile 提示");
}
void CCFile_testDlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
}
最后
以上就是妩媚冬瓜为你收集整理的VC++2015 CFile操作文件写入文字解决乱码的全部内容,希望文章能够帮你解决VC++2015 CFile操作文件写入文字解决乱码所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复