CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
英特尔®游戏设计大赛100美元现金周周送 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C++ 语言

是不是我的CString建的不对呀

楼主host()2003-12-01 20:41:23 在 C/C++ / C++ 语言 提问

首先我生成一个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  
  问题点数:0、回复次数:16Top

1 楼smalltalk(老徐)回复于 2003-12-01 21:48:22 得分 0

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

2 楼raygtr(朱仔)回复于 2003-12-01 22:28:54 得分 0

我同意楼上的~~~  
  代码的确是漏了CardPasswordStringIter的++操作~~~Top

3 楼host()回复于 2003-12-01 22:49:10 得分 0

不好意思,在问题里漏写了,CardPasswordStringIter++本来就是有的,但是仍然出现上述问题,Top

4 楼host()回复于 2003-12-02 09:14:10 得分 0

为什么没人回答呀,急Top

5 楼sunjguang(风卷残云)回复于 2003-12-02 09:29:06 得分 0

多半和这句话有关系:CString   temp_e=((CString)*CardPasswordStringIter);  
  你给temp_e直接赋值看看。我没有环境不能帮你测试。Top

6 楼Wolf0403(废人:独活十年~心如刀割)回复于 2003-12-02 09:39:49 得分 0

CString   temp_e=((CString)*CardPasswordStringIter);  
  这里从   vector   中读取的对象不需要进行强制类型转换就是   CString   型。  
   
  如果   for   中有   ++   操作还是这样,那么。。。爱莫能助了,呵呵Top

7 楼fireseed(【VC无敌,英明神武,千秋万代,一统江湖!】—奶油狗)回复于 2003-12-02 09:56:49 得分 0

取出的temp_e是对的,难道是写入的问题???  
   
  不知道你的输出结果有两列是什么意思Top

8 楼fireseed(【VC无敌,英明神武,千秋万代,一统江湖!】—奶油狗)回复于 2003-12-02 10:01:24 得分 0

取出的temp_e是对的,难道是写入的问题???  
   
  不知道你的输出结果有两列是什么意思Top

9 楼lhlhxb(就随风)回复于 2003-12-02 11:27:24 得分 0

在你给出的程序中看不出你有没有调用CreateRandString方法!Top

10 楼hxb74(开心小宝)回复于 2003-12-02 11:45:52 得分 0

瞎猜的,两个方面试试吧:  
  1,如果用的是两个线程的,查查是不是同步的问题。就是取的部分执行得快了,还没生成就取出了。  
  2,写的部分不要写字符串,只写字符试试。Top

11 楼host()回复于 2003-12-02 12:43:10 得分 0

我把所有的代码都贴出来,请高手们看看  
  //   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();  
   
   
   
   
  }  
  Top

12 楼host()回复于 2003-12-02 12:44:36 得分 0

程序是从OnCreate函数开始的Top

13 楼host()回复于 2003-12-02 12:49:55 得分 0

其中tempcard的输出是对的,但temppass的输出就是前面提及的问题Top

14 楼hxb74(开心小宝)回复于 2003-12-02 17:13:28 得分 0

你把CREATERANDSTRING里的ITOA部分也改成+'0'试试吧。Top

15 楼host()回复于 2003-12-02 19:34:05 得分 0

我加了,但是还是会出错,高手请帮忙呀Top

16 楼hxb74(开心小宝)回复于 2003-12-03 08:12:59 得分 0

那你在CREATERANDSTRING函数里的时候,temp和last字符串是对的吗?  
  在createrandserial函数里的时候,temppass是对的吗?Top

相关问题

  • CString
  • CString::Find,我怎么用也不对!郁闷
  • CString对象的小问题!
  • 如何在ATL建的exe中加入对mfc的支持,比如CString?
  • 为什么我的CString的一个对象不能连续的加入字串?
  • 我不想使用MFC框架,但想使用CString对象怎么办?
  • 如何构建动态二维数组比如CString类的数组:CString [n,m] ?
  • 怎么我用CString的Mid不行?
  • 我也来个关于CSTRING的问题.
  • CString &,CString * 和CString的问题

关键词

  • vector
  • virtual
  • cardpasswordstringiter
  • caboutdlg
  • cardpasswordstring
  • cstring
  • afx
  • createrandstring
  • 取出
  • cdialog

得分解答快速导航

  • 帖主:host

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo