永发信息网

MFC中用oftream流输出CString到文件中时,字符串变成十六制的?

答案:1  悬赏:60  手机版
解决时间 2021-04-14 06:06
  • 提问者网友:ミ烙印ゝ
  • 2021-04-13 07:08

#include "stdafx.h"
#include "Sequence.h"
#include "SequenceDlg.h"
#include<string>
#include<fstream>
#include<iostream>
#include<clocale>
#include<sstream>
#include<ctime>
using namespace std;
#define $ 80

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSequenceDlg dialog

CSequenceDlg::CSequenceDlg(CWnd* pParent )
: CDialog(CSequenceDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSequenceDlg)
m_fn1 = _T("");
m_fn2 = _T("");
m_match = 0;
m_mismatch = 0;
m_indel = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
pFileDlg1 = NULL;
pFileDlg2 = NULL;
FileName1 = "";
FileName2 = "";
// src_a = "";
// src_b = "";
}

void CSequenceDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSequenceDlg)
DDX_Text(pDX, IDC_FN1, m_fn1);
DDX_Text(pDX, IDC_FN2, m_fn2);
DDX_Text(pDX, IDC_MATCH, m_match);
DDX_Text(pDX, IDC_MISMATCH, m_mismatch);
DDX_Text(pDX, IDC_INDEL, m_indel);
DDV_MinMaxInt(pDX, m_indel, -10, 10);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSequenceDlg, CDialog)
//{{AFX_MSG_MAP(CSequenceDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_Sum_Of_Pair, OnButton3)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSequenceDlg message handlers

BOOL CSequenceDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here

return TRUE; // return TRUE unless you set the focus to a control
}

void CSequenceDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}

// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.

void CSequenceDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
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;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CSequenceDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}


void CSequenceDlg::opentxt1(CString &filename)
{
if (pFileDlg1!=NULL)
{
delete pFileDlg1;
}
pFileDlg1 = new CFileDialog(TRUE,".txt","文本文件",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"文本文件(*.txt)|*.txt|",NULL);// 创建打开对话框
pFileDlg1->DoModal();// 显示打开对话框
filename = pFileDlg1->GetPathName();// 获取文件名
}
void CSequenceDlg::opentxt2(CString &filename)
{
if (pFileDlg2!=NULL)
{
delete pFileDlg2;
}
pFileDlg2=new CFileDialog(FALSE,".txt","文本文件",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"文本文件(*.txt)|*.txt|",NULL);// 创建打开对话框
pFileDlg2->DoModal();// 显示打开对话框
filename = pFileDlg2->GetPathName();// 获取文件名
}


int CSequenceDlg::search(CString& chuan) //搜索在一行字符串中出现空格的结束位置
{
for(int i=0;i<=chuan.GetLength()-1;i++)
{
if(chuan[i]==' '&&chuan[i+1]!=' ')
{
position=i+1;
break;
}
}
return position;

}


CString CSequenceDlg::obtain(CString& chuan) //获取一条DNA序列的content内容
{
chuan.Delete(0,position-1);
return chuan;
}

void CSequenceDlg::OnButton1()
{
// TODO: Add your control notification handler code here
opentxt1(FileName1);
MessageBox(FileName1);
if (FileName1!="")
{
m_fn1 = FileName1;
}
UpdateData(FALSE);
}

void CSequenceDlg::OnButton2()
{
// TODO: Add your control notification handler code here
opentxt2(FileName2);
if (FileName2!="")
{
m_fn2 = FileName2;
}
UpdateData(FALSE);
}


void CSequenceDlg::OnButton3() //Sum_Of_Pair()函数
{
// TODO: Add your control notification handler code here
int match; //匹配值
int mismatch; //不匹配值
int indel;
char ch1[10],ch2[10],ch3[10];

GetDlgItem(IDC_MATCH)->GetWindowText(ch1,10); //
GetDlgItem(IDC_MISMATCH)->GetWindowText(ch2,10);
GetDlgItem(IDC_INDEL)->GetWindowText(ch3,10);

match=atoi(ch1);
mismatch=atoi(ch2);
indel=atoi(ch3);

CString content[$]; //定义space数量的字符串空间
long sum;
int f;
time_t startTime,endTime;
double costtime;

CTime time1=CTime::GetCurrentTime();
CString date1 = time1.Format("%Y-%m-%d %H:%M:%S %W-%A");
CStdioFile file;
//CString str;
file.Open(FileName1,CFile::modeRead,NULL);
//file.ReadString(str);


for(f=0;f<$;f++)
{

content[f]="";
}
CString chuan;
startTime=clock();
int i=1;
while(file.ReadString(chuan)) // 逐行读取
{ if(chuan[0]==' ')
break;
search(chuan);
chuan=obtain(chuan);
content[i]+=chuan;
i++; //循环结束时i-1就是一个文档里面字符串的条数
}
f=1;
file.ReadString(chuan); //行读取
while(file.ReadString(chuan))
{
if(chuan[0]!=' ')
{
f=f%i;
chuan=obtain(chuan);
content[f]+=chuan; //接上字符串
f++;
}
if(f%i==0)
{
f=f%i;
file.ReadString(chuan);
file.ReadString(chuan);
f++;
}
}

//用三个循环进行读取比较每个字符组,匹配则sum加match,不匹配则加mismatch,有一个为空格则加indel.
sum=0;
int K=i-1; //K为一个文件中DNA条数
int k;
int L=content[1].GetLength(); //L为一个字符串的列数
for(k=0;k<=L-1;k++)
for(int f=1;f<K;f++)
{
for(int j=f+1;j<=K;j++)
{
if(content[f][k]!='-'&&content[j][k]!='-')
{ if(content[f][k]==content[j][k])
sum+=match;
else
sum+=mismatch;
}
else if((content[f][k]!='-'&&content[j][k]=='-')||(content[f][k]=='-'&&content[j][k]!='-'))
{

sum+=indel;
}
else if(content[f][k]=='-'&&content[j][k]=='-')
continue;

}

}
cout<<sum<<endl;
endTime=clock();
CTime time2=CTime::GetCurrentTime();
CString date2 = time2.Format("%Y-%m-%d %H:%M:%S %W-%A");
costtime=(double)(endTime-startTime);

setlocale(LC_ALL," ");


ofstream ofs;

ofs.open(FileName2,ios::app); //打开文档
ofs <<"[filename]\n"<<FileName1<<"\n[SP]"<<sum<<endl<<"Start_Time:\n"<<date1<<endl<<"End_Time:\n"<<date2<<endl<<"Cost_Time:\n"<<costtime<<"ms"<<endl;
ofs.close();
}

最佳答案
  • 五星知识达人网友:低血压的长颈鹿
  • 2021-04-13 07:54
输出之前先转一下:
CString a( "text" );
比如 ofs << (LPCTSTR)a;
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯