15,980
社区成员
发帖
与我相关
我的任务
分享
char* old_locale = _strdup(setlocale(LC_CTYPE,NULL));
setlocale( LC_CTYPE, "chs"); // 在写文件之前,设置语言区域
try{
CStdioFile file;
file.Open(_T("F:\\11.txt"), CFile::modeWrite|CFile::modeNoTruncate);
file.SeekToEnd(); // 将文件指针定位到末尾
for(int i=0; i<2; i++)
{
file.WriteString(_T("Hello,World!!你好!!!\r\n"));
}
file.Close();
}
catch(CFileException* e)
{
e->ReportError();
e->Delete();
}
setlocale( LC_CTYPE, old_locale ); // 最后还原原来的设置
free(old_locale);
void CEditEx::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if( ::IsDBCSLeadByte(nChar) )
{
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
else
{
AfxMessageBox( _T("只能输入中文") );
}
}
void CMyEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if(nChar == 45)// 减号
{
CString str_;
GetWindowText(str_);
int NotEmpty = str_.GetLength();
int pos = str_.Find(_T("-"));
if(!pos)
nChar = 0;
else
{
if(!NotEmpty)
CEdit::OnChar(nChar, nRepCnt, nFlags);
else
nChar = 0;
}
}
if(nChar == 46 || nChar == 12290)//小数点 或中文句号
{
CString StrDot;
GetWindowText(StrDot);
int Pos = StrDot.Find(_T("."));
if(Pos>=0)
nChar = 0;
else
{
if(nChar == 12290)
{
nChar = 46;
nFlags = 52;
ReplaceSel(_T("."));
}
else
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
}
if(nChar == 8) //退格键
{
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
if(nChar<48 || nChar>57) //允许输入数字
nChar = 0;
else
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
#include <locale>
CStdioFile file;
file.Open(_T("F:\\11.txt"), CFile::modeCreate|CFile::modeReadWrite);
char* old_locale = _strdup(setlocale(LC_CTYPE,NULL));
setlocale( LC_CTYPE, "chs" );//设定区域为中文
file.WriteString(_T("Hello,World!!你好!!!"));//正常写入
setlocale( LC_CTYPE, old_locale );
free(old_locale);//还原区域先前设定
UpdateData( TRUE);
CStdioFile file(_T("EmpolyeeInfo.txt"), CFile::modeCreate|CFile::modeWrite);
CString str;
str+=m_name;
str+="\t";
str+=m_number;
str+="\t ";
if (m_male==-1)
{
MessageBox(_T("你没有选择职工的性别,请选择!"));
}
if (m_male==0){str+="男 ";str+="\t ";}
if (m_male==1){str+="女 ";str+="\t ";}
str+=m_age;
str+="\t ";
int index=m_Degree.GetCurSel();
CString strtemp;
m_Degree.GetLBText(index,strtemp);
str+=strtemp;
str+="\t ";
index=m_department.GetCurSel();
m_department.GetLBText(index,strtemp);
str+=strtemp;
str+="\t ";
str+=m_wage;
str+="\t ";
CString temp;
temp.Format(_T("%s\r\n"),str);
file.WriteString(temp);
file.Close();
void CEditEx::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if( ::IsDBCSLeadByte(nChar) )
{
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
else
{
AfxMessageBox( _T("只能输入中文") );
}
}