首页 > 代码库 > VC++6.0实现文本格式的转换保存
VC++6.0实现文本格式的转换保存
第一步:
建立mfc工程
第二步: 布置界面
第三步: 编辑按钮
下面是 选择对账文本并转换的代码实现
void CReorderDataDlg::OnButton1() { char sum_text[255], tmp[512]; long i; FILE *fp_s, *fp_t; char srcFileName[255], tagFileName[255]; char err_msg[255], szReadBuff[1024], szResultBuff[1024]; // TODO: Add your control notification handler code here CFileDialog dlg(TRUE,//TRUE是创建打开文件对话框,FALSE则创建的是保存文件对话框 "",//默认的打开文件的类型 NULL,//默认打开的文件名 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,//打开只读文件 "*(*.*)|*.*||"); //"文本文件(*.txt)|*.txt|所有文件 (*.*)|*.*||"); if(dlg.DoModal()==IDOK) { CString m_FilePath = dlg.GetPathName();////////取出文件路径 CString m_path; m_path=m_FilePath;//将文件的路径放入m_path memset( srcFileName, 0x00, sizeof(srcFileName) ); strcpy( srcFileName, m_path.GetBuffer(255)); UpdateData(FALSE); } else return; memset( tagFileName, 0x00, sizeof(tagFileName)); memcpy( tagFileName, srcFileName+strlen(srcFileName)-8, 8 ); strcat( tagFileName, ".txt"); // AfxMessageBox( tagFileName ); memset( err_msg, 0x00, sizeof(err_msg)); fp_s = fopen( srcFileName, "r"); if ( fp_s == NULL) { sprintf( err_msg, "FILE[%s] OPEN FAILED!", srcFileName); return; } fp_t = fopen( tagFileName, "w"); if ( fp_t == NULL) { sprintf( err_msg, "FILE[%s] OPEN FAILED!", tagFileName); return; } i=0; while( !feof(fp_s) ) { memset( szReadBuff, 0x00, sizeof(szReadBuff)); memset( szResultBuff, 0x00, sizeof(szResultBuff)); fgets( szReadBuff, 512, fp_s ); if ( feof(fp_s)) break; str_alltrim( szReadBuff ); // AfxMessageBox( szReadBuff ); if (szReadBuff[0]< 0x30 || szReadBuff[0] >0x39 ) { continue; } //处理成标准的清算文件 /*交易日期*/ memset( tmp, 0x00, sizeof(tmp) ); if( !str_find_field( szReadBuff, FLAG, 11, tmp ) ) { strcat( szResultBuff, tmp ); strcat( szResultBuff, " " ); } /*交易时间*/ memset( tmp, 0x00, sizeof(tmp) ); if( !str_find_field( szReadBuff, FLAG, 12, tmp ) ) { char value_tmp[12]; memset( value_tmp,0x00, sizeof(value_tmp) ); memcpy( value_tmp, tmp, 6 ); strcat( szResultBuff, value_tmp ); strcat( szResultBuff, " " ); } /*终端号*/ memset( tmp, 0x00, sizeof(tmp) ); if( !str_find_field( szReadBuff, FLAG, 3, tmp ) ) { strcat( szResultBuff, tmp ); strcat( szResultBuff, " " ); } /*卡号--要特殊处理*/ char tmp_str[40]; memset( tmp, 0x00, sizeof(tmp) ); memset( tmp_str, 0x00, sizeof(tmp_str) ); if( !str_find_field( szReadBuff, FLAG, 1, tmp ) ) { sprintf( tmp_str, "%-19.19s", tmp ); strcat( szResultBuff, tmp_str ); strcat( szResultBuff, " " ); } /*金额*/ memset( tmp, 0x00, sizeof(tmp) ); if( !str_find_field( szReadBuff, FLAG, 7, tmp ) ) { strcat( szResultBuff, tmp ); strcat( szResultBuff, " " ); } /*参考号*/ memset( tmp, 0x00, sizeof(tmp) ); if( !str_find_field( szReadBuff, FLAG, 10, tmp ) ) { strcat( szResultBuff, tmp ); strcat( szResultBuff, " " ); } /*交易类型*/ memset( tmp, 0x00, sizeof(tmp) ); if( !str_find_field( szReadBuff, FLAG, 6, tmp ) ) { if( strcmp( tmp, "0103") ) { strcat( szResultBuff, "退货" ); strcat( szResultBuff, " " ); } else { strcat( szResultBuff, "消费" ); strcat( szResultBuff, " " ); } } /*发卡行*/ strcat( szResultBuff, "银联" ); strcat( szResultBuff, "\n"); fputs( szResultBuff, fp_t ); i++; memset( sum_text, 0x00, sizeof(sum_text)); sprintf( sum_text, "正在处理第%d笔记录",i ); GetDlgItem(IDC_STATIC)->SetWindowText(sum_text); UpdateData(FALSE); } fclose(fp_s); fclose(fp_t); sprintf( sum_text, "已处理%d笔,处理完成",i ); GetDlgItem(IDC_STATIC)->SetWindowText(sum_text); UpdateData(FALSE); return; }
其中str_find_fild函数通过定义实现
class CReorderDataDlg : public CDialog
{
// Construction
public:
CReorderDataDlg(CWnd* pParent = NULL);// standard constructor
//int change_file( char *filein, char *fileout );
int str_find_field( char *buf, char *delimer, int fld_no, char *field );
具体实现为
int CReorderDataDlg::str_find_field( char *buf, char *delimer, int fld_no, char *field ) { char *p1, *p2, ch; int i; p1 = buf; for( i=0;i<fld_no-1;i++ ) { p1 = strstr( p1, delimer ); if( !p1 ) /* 没有找到 */ return -1; p1 += strlen( delimer ); } p2 = strstr( p1, delimer ); if( !p2 ) strcpy( field, p1 ); else { ch = *p2; *p2 = 0; strcpy( field, p1 ); *p2 = ch; } str_alltrim(field); return 0; }
void CReorderDataDlg::OnButton1() {char sum_text[255], tmp[512];long i;FILE *fp_s, *fp_t;char srcFileName[255], tagFileName[255];char err_msg[255], szReadBuff[1024], szResultBuff[1024];
// TODO: Add your control notification handler code hereCFileDialog dlg(TRUE,//TRUE是创建打开文件对话框,FALSE则创建的是保存文件对话框 "",//默认的打开文件的类型 NULL,//默认打开的文件名 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,//打开只读文件 "*(*.*)|*.*||");//"文本文件(*.txt)|*.txt|所有文件 (*.*)|*.*||");if(dlg.DoModal()==IDOK) { CString m_FilePath = dlg.GetPathName();////////取出文件路径 CString m_path; m_path=m_FilePath;//将文件的路径放入m_path memset( srcFileName, 0x00, sizeof(srcFileName) );strcpy( srcFileName, m_path.GetBuffer(255));UpdateData(FALSE); }elsereturn;
memset( tagFileName, 0x00, sizeof(tagFileName));memcpy( tagFileName, srcFileName+strlen(srcFileName)-8, 8 ); strcat( tagFileName, ".txt"); // AfxMessageBox( tagFileName );
memset( err_msg, 0x00, sizeof(err_msg));fp_s = fopen( srcFileName, "r");if ( fp_s == NULL){sprintf( err_msg, "FILE[%s] OPEN FAILED!", srcFileName);return;}
fp_t = fopen( tagFileName, "w");if ( fp_t == NULL){sprintf( err_msg, "FILE[%s] OPEN FAILED!", tagFileName);return;}
i=0;while( !feof(fp_s) ){memset( szReadBuff, 0x00, sizeof(szReadBuff));memset( szResultBuff, 0x00, sizeof(szResultBuff));fgets( szReadBuff, 512, fp_s );if ( feof(fp_s)) break; str_alltrim( szReadBuff );//AfxMessageBox( szReadBuff ); if (szReadBuff[0]< 0x30 || szReadBuff[0] >0x39 ){continue;}
//处理成标准的清算文件/*交易日期*/memset( tmp, 0x00, sizeof(tmp) );if( !str_find_field( szReadBuff, FLAG, 11, tmp ) ){strcat( szResultBuff, tmp );strcat( szResultBuff, "" );}/*交易时间*/memset( tmp, 0x00, sizeof(tmp) ); if( !str_find_field( szReadBuff, FLAG, 12, tmp ) ){char value_tmp[12];memset( value_tmp,0x00, sizeof(value_tmp) );memcpy( value_tmp, tmp, 6 );strcat( szResultBuff, value_tmp );strcat( szResultBuff, "" );} /*终端号*/memset( tmp, 0x00, sizeof(tmp) ); if( !str_find_field( szReadBuff, FLAG, 3, tmp ) ){strcat( szResultBuff, tmp );strcat( szResultBuff, "" );}
/*卡号--要特殊处理*/char tmp_str[40];memset( tmp, 0x00, sizeof(tmp) );memset( tmp_str, 0x00, sizeof(tmp_str) ); if( !str_find_field( szReadBuff, FLAG, 1, tmp ) ){sprintf( tmp_str, "%-19.19s", tmp );strcat( szResultBuff, tmp_str );strcat( szResultBuff, "" );}/*金额*/memset( tmp, 0x00, sizeof(tmp) ); if( !str_find_field( szReadBuff, FLAG, 7, tmp ) ){strcat( szResultBuff, tmp );strcat( szResultBuff, "" );}/*参考号*/memset( tmp, 0x00, sizeof(tmp) );if( !str_find_field( szReadBuff, FLAG, 10, tmp ) ){strcat( szResultBuff, tmp );strcat( szResultBuff, "" );}/*交易类型*/memset( tmp, 0x00, sizeof(tmp) );if( !str_find_field( szReadBuff, FLAG, 6, tmp ) ){if( strcmp( tmp, "0103") ){strcat( szResultBuff, "退货" );strcat( szResultBuff, "" );}else{strcat( szResultBuff, "消费" );strcat( szResultBuff, "" );}}/*发卡行*/strcat( szResultBuff, "银联" );strcat( szResultBuff, "\n"); fputs( szResultBuff, fp_t );i++;memset( sum_text, 0x00, sizeof(sum_text));sprintf( sum_text, "正在处理第%d笔记录",i );
GetDlgItem(IDC_STATIC)->SetWindowText(sum_text);UpdateData(FALSE);}
fclose(fp_s);fclose(fp_t);sprintf( sum_text, "已处理%d笔,处理完成",i );GetDlgItem(IDC_STATIC)->SetWindowText(sum_text);UpdateData(FALSE);
return;
}
VC++6.0实现文本格式的转换保存