是不是我的CString建的不对呀

host 2003-12-01 08:41:23
首先我生成一个CString
CString CTestDlg::CreateRandString(int length)
{

int i=0;
CString temp="";
CString last="";
char createrand[1];
srand(unsigned(time(NULL)));
for(int m=0;m<length;m++){
i=rand();
i=i%9;
itoa(i,createrand,10);
temp+=createrand;
}
last=temp;
return last;


}
接着压入
vector<CString>CardPasswordString;
vector<CString>::iterator CardPasswordStringIter;
CardNumberString.push_back(temppass);
然后取出放入文件中

for(CardPasswordStringIter=CardPasswordString.begin();CardPasswordStringIter!=CardPasswordString.end();)
{

CString temp_e=((CString)*CardPasswordStringIter);
ar.WriteString(temp_e);//sr为CArchive类
}
为什么写入文件后无论VECTOR多少长,temp_e都为同一值,而且不是我生成的随机数,而是不知道那里出来的一个数,是不是我的CString建的不对呀,但是单步跟踪取出的temp_e是对的呀,请问高手是为什么?
例如输出如下,第一列都为一样,但长度是对的
8132730621 00000003
8132730621 00000004
8132730621 00000005
8132730621 00000006
8132730621 00000007
8132730621 00000008
8132730621 00000009
8132730621 00000010
8132730621 00000011
8132730621 00000012
8132730621 00000013
8132730621 00000014
8132730621 00000015
8132730621 00000016
8132730621 00000017
...全文
45 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
hxb74 2003-12-03
  • 打赏
  • 举报
回复
那你在CREATERANDSTRING函数里的时候,temp和last字符串是对的吗?
在createrandserial函数里的时候,temppass是对的吗?
host 2003-12-02
  • 打赏
  • 举报
回复
其中tempcard的输出是对的,但temppass的输出就是前面提及的问题
host 2003-12-02
  • 打赏
  • 举报
回复
程序是从OnCreate函数开始的
host 2003-12-02
  • 打赏
  • 举报
回复
我把所有的代码都贴出来,请高手们看看
// testDlg.cpp : implementation file
#include "stdafx.h"
#include "test.h"
#include "testDlg.h"
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <ctime>
#include <iostream>
//#include <fstream>
using namespace std;

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
vector<CString>CardNumberString;
vector<CString>::iterator CardNumberStringIter;
vector<CString>CardPasswordString;
vector<CString>::iterator CardPasswordStringIter;
/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CTestDlg dialog

CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTestDlg)
m_begincode = 0.0;
m_endcode = 0.0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTestDlg)
DDX_Text(pDX, IDC_BEGINCODE, m_begincode);
DDV_MinMaxDouble(pDX, m_begincode, 0., 1.e+104);
DDX_Text(pDX, IDC_ENDCODE, m_endcode);
DDV_MinMaxDouble(pDX, m_endcode, 0., 1.e+033);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
//{{AFX_MSG_MAP(CTestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnCreate)
ON_BN_CLICKED(IDC_CANCEL, OnClickCancel)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestDlg message handlers

BOOL CTestDlg::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 CTestDlg::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 CTestDlg::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 CTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

void CTestDlg::OnCreate()
{

// TODO: Add your control notification handler code here

UpdateData(true);
endcode=m_endcode;
begincode=m_begincode;
CreateRandSerial();
WriteToDisk();
// CreateRandString(i);
// CreateRandStruct();



}



void CTestDlg::CreateRandStruct()
{

}

void CTestDlg::OnCancel()
{
// TODO: Add extra cleanup here

// CDialog::OnCancel();
}

void CTestDlg::OnClickCancel()
{
// TODO: Add your control notification handler code here
CDialog::OnCancel();

}



CString CTestDlg::CreateRandString(int length)
{

int i=0;
CString temp="";
CString last="";
char createrand[1];
srand(unsigned(time(NULL)));
for(int m=0;m<length;m++){
i=rand();
i=i%9;
itoa(i,createrand,10);
temp+=createrand;
}
last=temp;
return last;


}

CString CTestDlg::DoubleConvertCString(double a)
{
int temp;
temp=(int)a;
CString str;
CString zero;
zero='0';
str.Format("%d",temp);
while (str.GetLength()<8) {
str=zero+str;
}
return str;
}

void CTestDlg::CreateRandSerial()
{

double count=0;
int length=10;
CString tempcard;
CString temppass;
count=endcode-begincode;
for(int i=0;i<count;i++){
temppass=CreateRandString(length);
tempcard=DoubleConvertCString(begincode);
begincode=begincode+1;
CardNumberString.push_back(tempcard);
CardNumberString.push_back(temppass);

}

}

void CTestDlg::WriteToDisk()
{
CString bigtest="\r\n";

CString temp_space=" ";
CString temp_e="";
CString temp_t="";
CFile fout;
char *buf_1;
char *buf_2;
char buf_3[100];
char key[]={0,2,0,0,9,3,5,1,9,8,0,0,9,1,7},buf[255];
fout.Open("d:\\outfile.ini",CFile.modeWrite|CFile.modeCreate);
if(fout==NULL){
fout.Close();
MessageBox("输出文件未找到","提示",MB_ICONWARNING);
exit ;
}


CArchive ar(&fout,CArchive::store);
ar.GetFile()->Seek(0,CFile::begin);

for(CardNumberStringIter=CardNumberString.begin();CardNumberStringIter!=CardNumberString.end();)
{


temp_t=((CString)*CardNumberStringIter);

CardNumberStringIter++;
temp_e=*CardNumberStringIter;

ar.WriteString(temp_e);
ar.WriteString(temp_space);
ar.WriteString(temp_t);
ar.WriteString(bigtest);
CardNumberStringIter++;



}

ar.Close();
fout.Close();




}
hxb74 2003-12-02
  • 打赏
  • 举报
回复
瞎猜的,两个方面试试吧:
1,如果用的是两个线程的,查查是不是同步的问题。就是取的部分执行得快了,还没生成就取出了。
2,写的部分不要写字符串,只写字符试试。
就随风 2003-12-02
  • 打赏
  • 举报
回复
在你给出的程序中看不出你有没有调用CreateRandString方法!
fireseed 2003-12-02
  • 打赏
  • 举报
回复
取出的temp_e是对的,难道是写入的问题???

不知道你的输出结果有两列是什么意思
fireseed 2003-12-02
  • 打赏
  • 举报
回复
取出的temp_e是对的,难道是写入的问题???

不知道你的输出结果有两列是什么意思
Wolf0403 2003-12-02
  • 打赏
  • 举报
回复
CString temp_e=((CString)*CardPasswordStringIter);
这里从 vector 中读取的对象不需要进行强制类型转换就是 CString 型。

如果 for 中有 ++ 操作还是这样,那么。。。爱莫能助了,呵呵
sunjguang 2003-12-02
  • 打赏
  • 举报
回复
多半和这句话有关系:CString temp_e=((CString)*CardPasswordStringIter);
你给temp_e直接赋值看看。我没有环境不能帮你测试。
host 2003-12-02
  • 打赏
  • 举报
回复
为什么没人回答呀,急
host 2003-12-02
  • 打赏
  • 举报
回复
我加了,但是还是会出错,高手请帮忙呀
hxb74 2003-12-02
  • 打赏
  • 举报
回复
你把CREATERANDSTRING里的ITOA部分也改成+'0'试试吧。
host 2003-12-01
  • 打赏
  • 举报
回复
不好意思,在问题里漏写了,CardPasswordStringIter++本来就是有的,但是仍然出现上述问题,
raygtr 2003-12-01
  • 打赏
  • 举报
回复
我同意楼上的~~~
代码的确是漏了CardPasswordStringIter的++操作~~~
smalltalk 2003-12-01
  • 打赏
  • 举报
回复
CardPasswordStringIter需要++ !即:

for(CardPasswordStringIter=CardPasswordString.begin();CardPasswordStringIter!=CardPasswordString.end(); CardPasswordString++)
{
CString temp_e=((CString)*CardPasswordStringIter);
ar.WriteString(temp_e);//sr为CArchive类
}

64,636

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧