⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainfrm.cpp

📁 专门处理UNICODE编码文件的工作区
💻 CPP
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "try_unic.h"

#include "MainFrm.h"
#include "mycode.h"

#include "WORDSEARCH.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_COMMAND(ID_UNIC, OnUnic)
	ON_COMMAND(ID_TEXT, OnText)
	ON_COMMAND(ID_TRIMSPACE, OnTrimspace)
	ON_COMMAND(ID_GBKTOUNIC, OnGbktounic)
	ON_COMMAND(ID_CSTDIOFILE, OnCstdiofile)
	ON_COMMAND(ID_GBK2UNIC, OnGbk2unic)
	ON_COMMAND(ID_G2U, OnG2u)
	ON_COMMAND(ID_G2U_S, OnG2uS)
	ON_COMMAND(ID_ITEMSCITIAO, OnItemscitiao)
	ON_COMMAND(ID_ITEMSCIXING, OnItemscixing)
	ON_COMMAND(ID_FILES2ONE, OnFiles2one)
	ON_COMMAND(ID_SWORD, OnSword)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CMDIFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CMDIFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CMDIFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers


void CMainFrame::OnUnic() 
{
	// TODO: Add your command handler code here
//	wchar_t a[100]=L"你好";
	int i=1,j=2;
	AfxMessageBox(L"你好");
	CString a="dfsdf";//这里的CString已经是宽的了
	AfxMessageBox(a);
	CString b;
	b.Format(L"%d",a.Find(L"df"));
	a.FormatMessage(L"12345");//FormatMessage()重新定义
//	a.LoadString();从句柄中加载字符串

//	a.FormatV(L"%d%d%d",i,j,i);
	b=a.GetAt(2);//GetAt(x)返回下标为X的字符
	a.Insert(2,L"789");//Insert(2,L"789");在第二个元素后插入字符串
//	a.Delete(2);//Delete(x)删除下标为x的元素
//	a.Empty();//Empty(),清空
//	b.Format(L"%d",a.FindOneOf(L"st"));//FindOneOf()相当于wcswcs
//	b.Format(L"%d",a.Find(L"df"));//find(),返回下标
	a.Right(2);
	AfxMessageBox(a);

}

void CMainFrame::OnText() 
{
	// TODO: Add your command handler code here
	CFileDialog dlg(TRUE, L"txt", L"*.txt");
	if(dlg.DoModal()!=IDOK) return;
//	dlg.FlashWindow(TRUE);
//	MyTextFile  file(dlg.GetPathName());
	CString path,filename;
//	AfxMessageBox(dlg.GetPathName());
	path=dlg.GetFileName();
	filename=dlg.GetFileTitle()+L"re_.txt";
	CFile out;
	if(!out.Open(L"freq.txt",CFile::modeWrite|CFile::modeCreate)){
		AfxMessageBox(L"Can't open file!");
		return;
	}
//	out.Write(L"sdfdfdfsdfrer",100);
	CFile in;	
	if(!in.Open(path,CFile::modeRead)){
		AfxMessageBox(L"Can't open file!"+path);
		return;
	}
	DWORD m_nlen=in.GetLength();

	wchar_t *text=new wchar_t[m_nlen];
	in.Read (text,m_nlen);//read
	out.Write(text,m_nlen);//write
	fcloseall();
	AfxMessageBox(L"ok!");
}

void CMainFrame::OnTrimspace() 
{
	// TODO: Add your command handler code here
	CFileDialog dlg(TRUE, L"txt", L"*.txt");
	if(dlg.DoModal()!=IDOK) return;
	CString box;
	CString path=dlg.GetPathName();
	wchar_t *text;
	int n=LoadUnicText(text,path);
	box.Format(L"加载字符%d个",n);
	AfxMessageBox(box);
	FILE* output1=_wfopen(L"reunic.txt",L"wb");
	FILE* output2=_wfopen(L"re.txt",L"wb");
	setlocale(LC_ALL,"chs");
	if(text!=0){
	fputwc(65279,output1);//输出unic标记
		while(*text){
			if(*text!=L' '){
				fputwc(*text,output1);//生成unic文本
			//	fwrite(text,1,2,output1);
				fprintf(output2,"%lc",*text);//生成纯文本
				text++;
			}
			else text++;
		}
	}
	fcloseall();
	AfxMessageBox(L"处理完毕!");
	
}

void CMainFrame::OnGbktounic() 
{//
	// TODO: Add your command handler code here
	CFileDialog dlg(TRUE, L"txt", L"*.*");
	if(dlg.DoModal()!=IDOK) return;
	CString path,filename;
//	AfxMessageBox(dlg.GetPathName());
	path=dlg.GetFileName();
	filename=dlg.GetFileTitle()+L"re_.txt";
	CFile out;
	if(!out.Open(filename,CFile::modeWrite|CFile::modeCreate|CFile::typeBinary)){
		AfxMessageBox(L"Can't open file!");
		return;
	}
	CFile in;	
	if(!in.Open(path,CFile::modeRead|CFile::typeBinary)){
		AfxMessageBox(L"Can't open file!"+path);
		return;
	}
	DWORD m_nlen=in.GetLength();
	CString	box;
	box.Format(L"加载字符%d个",m_nlen);
	AfxMessageBox(box);

	char *text=new char[m_nlen+1];
/*	wchar_t *p=text;
	wchar_t ch;
	DWORD flen=0;
	FILE* in2=_wfopen(L"article219602449.shtml",L"rb");
	setlocale(LC_ALL,"chs");
	while((ch=fgetwc(in2))!=WEOF){
		*p++=ch;
	}
	*p=0;
	FILE* out2=_wfopen(L"re.txt",L"wb");
	fwrite(text,p-text,2,out2);
*/
	CString tmp;
	in.Read(text,m_nlen);
//	tmp.LoadString(in);
//	in.Read(&tmp,m_nlen);
	text[m_nlen]='\0';
	FILE* out2=_wfopen(L"rrre.txt",L"wb");
	fwrite(text,m_nlen,1,out2);//output text as mbs

//	tmp=CString(text);
	box.Format(L"加载字符%d个",tmp.GetLength());
	AfxMessageBox(box);
	wchar_t unic_tag=65279;
	out.Write(&unic_tag,2);//输出unic标记

//		setlocale(LC_ALL,"chs");
//	mbstowcs(text2,text,m_nlen+1);


int n=MultiByteToWideChar(CP_ACP, 0,text, -1, NULL, NULL);
	wchar_t *text2=new wchar_t[2*n];
	MultiByteToWideChar(CP_ACP, 0,text,m_nlen,text2,2*n);
box.Format(L"加载字符%d个",wcslen(text2));
	AfxMessageBox(box);
//	out.Write(tmp,tmp.GetLength());
	out.Write(text2,2*n-2);
	out.Flush();
	in.Close();
	out.Close();
//	CString tmp=CString(text);//利用Cstring做类型转换
//	out.WriteHuge(tmp,tmp.GetLength());
/*	wchar_t *tmp=new wchar_t[m_nlen];
	mbstowcs(tmp,text,m_nlen);
	out.Write(tmp,m_nlen);
	out.Close();
*/
/*	char *p=text,*from,*to;
	while(*p){
		if(*p==L'>'){
			from=p+1;
			while(*p++!=L'<');
			to=p-1;
			out.Write(from,to-from);
			out.Write(L"\n",1);
		}
		p++;
	}
*/	
	fcloseall();
	AfxMessageBox(L"ok!");


}

void CMainFrame::OnCstdiofile() 
{
	// TODO: Add your command handler code here
	CStdioFile f1;
	CString text="hello阿 的发";
	f1.Open(L"reee.txt",CFile::modeCreate | CFile::modeWrite);
	f1.WriteString(L"你好dfsdfewgjui");
	f1.WriteString(text);
}

void CMainFrame::OnGbk2unic() 
{//直接处理单个文件,不调用自编函数
	// TODO: Add your command handler code here
	FILE *in,*out;
	setlocale(LC_ALL,"chs");
	if((in=_wfopen(L"re.txt",L"rt"))==NULL){
		AfxMessageBox(L"Can't open file!");
		return;
	}
	if((out=_wfopen(L"re_re.txt",L"wb"))==NULL){
		AfxMessageBox(L"Can't open file!");
		return;
	}
	int  length =_filelength(_fileno(in));
	wchar_t ch;
	fputwc(65279,out);//输出unic标记
	for(int i=0; (ch=fgetwc(in))!=WEOF && i<length; i++){
		fputwc(ch,out);
	}
	fcloseall();
	AfxMessageBox(L"处理完毕!");
}

void CMainFrame::OnG2u() 
{//处理单个文件,使用fgetwc()
	// TODO: Add your command handler code here
	CFileDialog dlg(TRUE, L"txt", L"*.txt");
	if(dlg.DoModal()!=IDOK) return;
	CString in=dlg.GetPathName();

/*	CFileDialog dlg2(FALSE, L"txt", L"*.txt");
	if(dlg.DoModal()!=IDOK) return;
	CString out=dlg2.GetPathName();
	//不知道为什么不能打开文件
*/
	CString out=dlg.GetFileTitle();
	out+=L"_re.txt";
	const wchar_t *inn=(LPCWSTR)in;
	const wchar_t *outn=(LPCWSTR)out;
	gbk2uni(inn,outn);
	AfxMessageBox(L"处理完毕!");
}

void CMainFrame::OnG2uS() 
{//处理多个文件,使用fgetwc()
	// TODO: Add your command handler code here
	CFileDialog dlg(TRUE,L"txt", L"*.txt",OFN_ALLOWMULTISELECT);
	if(dlg.DoModal()!=IDOK) return;
	int file_num=0;
	CString filename,outname,msg;
	POSITION pos=dlg.GetStartPosition();
	while(pos!=NULL){
		filename=dlg.GetNextPathName(pos);
		outname=filename;
		int i=outname.ReverseFind(L'\\');
		if (i<0) return;//没有文件名则退出
		outname=outname.Mid(i+1);
		outname=L"unic_"+outname;

		const wchar_t *inn=(LPCWSTR)filename;//
		const wchar_t *outn=(LPCWSTR)outname;
		gbk2uni(inn,outn);
		file_num++;
	}
	msg.Format(L"处理文件%d个",file_num);
	AfxMessageBox(msg);
}

void CMainFrame::OnItemscitiao() 
{
	// TODO: Add your command handler code here
	CFileDialog dlg(TRUE, L"txt", L"*.txt");
	if(dlg.DoModal()!=IDOK) return;
	CString path=dlg.GetPathName();
	wchar_t *text=NULL;
	int filelen=LoadUnicText(text,path);
	CString msg;
	msg.Format(L"加载字符%d个",filelen);
	AfxMessageBox(msg);

	int i=0;
	wchar_t *p=text;
	while(*p){
		if(*p==L'/' && *(p+1)<128)
			i++;//统计词数
		p++;
	}
	msg.Format(L"文本中有词例%d个",i);
	AfxMessageBox(msg);
	wchar_t **pp;
	pp=GetUnicStringArray(text,filelen);
	AfxMessageBox(L"排序结束!");

	CString outname=path;
		int ii=outname.ReverseFind(L'\\');
		if (ii<0) return;//没有文件名则退出
		outname=outname.Mid(ii+1);
		outname=L"words_"+outname;

	FILE *out=_wfopen(outname,L"wb");
/*	setlocale(LC_ALL,"chs");
	for(int m=0;m<i;m++)
		for(int n=0;n<20;n++){
			if (pp[m][n]==L'/')
				break;
			fprintf(out,"%lc",pp[m][n]);
			
		}
*/
	WordItem *items=new WordItem[i];//分配词表的结构数组
	GetItems(items,pp,i,out);
	fclose(out);
	fcloseall();
	AfxMessageBox(L"处理完毕!");
}

void CMainFrame::OnItemscixing() 
{//
	// TODO: Add your command handler code here

	CFileDialog dlg(TRUE, L"txt", L"*.txt");
	if(dlg.DoModal()!=IDOK) return;
	CString path=dlg.GetPathName();
	wchar_t *text=NULL;
	int filelen=LoadUnicText(text,path);
	CString msg;
	msg.Format(L"加载字符%d个",filelen);
	AfxMessageBox(msg);

	int i=0;
	wchar_t *p=text;
	while(*p){
		if(*p==L'/' && *(p+1)<128)
			i++;//统计词数
		p++;
	}
	msg.Format(L"文本中有词例%d个",i);
	AfxMessageBox(msg);
	wchar_t **pp;
	pp=GetUnicStringArray(text,filelen);
	AfxMessageBox(L"排序结束!");

	CString outname=path;
		int ii=outname.ReverseFind(L'\\');
		if (ii<0) return;//没有文件名则退出
		outname=outname.Mid(ii+1);
		outname=L"items_"+outname;

	FILE *out=_wfopen(outname,L"wb");
/*	setlocale(LC_ALL,"chs");
	for(int m=0;m<i;m++)
		for(int n=0;n<20;n++){
			if (pp[m][n]==L'/')
				break;
			fprintf(out,"%lc",pp[m][n]);
			
		}
*/
	WordItem *items=new WordItem[i];//分配词表的结构数组
	GetItems2(items,pp,i,out);
	fclose(out);
	fcloseall();
	AfxMessageBox(L"处理完毕!");
}

void CMainFrame::OnFiles2one() 
{
	// TODO: Add your command handler code here
	CFileDialog dlg(TRUE,L"unicode", L"*.unicode",OFN_ALLOWMULTISELECT | OFN_ENABLESIZING ,L"文本|*.txt|UNICODE|*.unicode|");
	if(dlg.DoModal()!=IDOK){
		AfxMessageBox(L"取消或失败!");
		return;
	}
	int file_num=0,char_num=0;
	CString filename,outname,msg;
	POSITION pos=dlg.GetStartPosition();
	const wchar_t *inn;
//	const wchar_t *outn;
	wchar_t *text=NULL;
	FILE *out;
	out=_wfopen(L"total.txt",L"wb");
	setlocale(LC_ALL,"chs");
	while(pos!=NULL){
		filename=dlg.GetNextPathName(pos);
//		outname=filename;
//		int i=outname.ReverseFind(L'\\');
//		if (i<0) return;//没有文件名则退出
//		outname=outname.Mid(i+1);
//		outname=L"total_"+outname;

		inn=(LPCWSTR)filename;//
//		outn=(LPCWSTR)outname;
		
		char_num+=writefile(inn,out);
		file_num++;
	}

//	fprintf(out,"%ls",text);
	fcloseall();
	msg.Format(L"处理文件%d个",file_num);
	AfxMessageBox(msg);
	
}

void CMainFrame::OnSword() 
{
	// TODO: Add your command handler code here
	WORDSEARCH search;
	search.DoModal();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -