std::vector出现乱码的问题

yaphare 2013-10-25 09:51:39
过程是:使用ADO从数据库中读取数据,然后保存到结构中,再把结构保存到std::vector中,问题是:当保存第二行数据的时候,第一行的数据就变成乱码了。请大侠给与指教。代码如下:
std::vector<tagKuCun> vecKuCun;

typedef struct _tagKuCun
{
LPCTSTR szGoodsCode;
LPCTSTR szGoodsName;
LPCTSTR szGoodsType;
LPCTSTR szGoodsUnit;
int nGoodsNumber;

}tagKuCun;

...
if(m_pADO->Execute(sqlSelectTable))
{
tagKuCun *strTmpKuCun=new tagKuCun();
while(0== m_pADO->GetRecordset()->adoEOF)
{

CString csTmpGC=(LPCTSTR)(_bstr_t)m_pADO->GetRecordset()->GetCollect("GoodsCode");
CString csTmpGN = (LPCTSTR)(_bstr_t)m_pADO->GetRecordset()->GetCollect("GoodsName");
CString csTmpGU = (LPCTSTR)(_bstr_t)m_pADO->GetRecordset()->GetCollect("GoodsUnit");
CString csTmpUnits = (LPCTSTR)(_bstr_t)m_pADO->GetRecordset()->GetCollect("Units");
int nTmpNumber=m_pADO->GetRecordset()->GetCollect("Number");

strTmpKuCun->szGoodsCode =csTmpGC;
strTmpKuCun->szGoodsName=csTmpGN;
strTmpKuCun->szGoodsType =csTmpGU;
strTmpKuCun->szGoodsUnit=csTmpUnits;
strTmpKuCun->nGoodsNumber=nTmpNumber;

vecKuCun.push_back(*strTmpKuCun); //这里循环第二次的时候,上次保存的数据就变成乱码了??(貌似是指针初始化了)
m_pADO->GetRecordset()->MoveNext();
}
}
...全文
613 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
爆豆 2013-10-25
  • 打赏
  • 举报
回复
tagKuCun *strTmpKuCun=new tagKuCun(); 你敢用new之后不delete,写代码忌讳这个。换成这个:tagKuCun strTmpKuCun;不是挺好吗。
ztenv 版主 2013-10-25
  • 打赏
  • 举报
回复
引用 2 楼 yaphare 的回复:
非常感谢,那要怎么改才能实现想要的功能呢? [quote=引用 1 楼 lianshaohua 的回复:] 1、先看一下LPCTSTR代表了什么 2、接着你应该应该明白了MoveNext()之后为什么第一个结构体会乱码了 可先不使用vector,你先声明两个结构体实例,按你的方法读到第二个,这时再看第一个结构体变量
[/quote] 你的结构中存储的是指针,读取的时候为指针分配内存,然后copy m_pADO->GetRecordset()->GetCollect("GoodsCode");这些字段中的数据,再试试看
kivien 2013-10-25
  • 打赏
  • 举报
回复
项目属性中的字符集问题?
yaphare 2013-10-25
  • 打赏
  • 举报
回复
非常感谢,那要怎么改才能实现想要的功能呢?
引用 1 楼 lianshaohua 的回复:
1、先看一下LPCTSTR代表了什么 2、接着你应该应该明白了MoveNext()之后为什么第一个结构体会乱码了 可先不使用vector,你先声明两个结构体实例,按你的方法读到第二个,这时再看第一个结构体变量
ztenv 版主 2013-10-25
  • 打赏
  • 举报
回复
1、先看一下LPCTSTR代表了什么 2、接着你应该应该明白了MoveNext()之后为什么第一个结构体会乱码了 可先不使用vector,你先声明两个结构体实例,按你的方法读到第二个,这时再看第一个结构体变量
Build 1632
requires software maintenance through 2008.04.03. (Beta release.)
New! Added context-sensitive help to VA X dialogs. Click ? or press F1 to get help. (case=8980)
New! Visual Studio 2008-style transparent listboxes are available in all IDEs. Hold down Ctrl when listbox is displayed to activate. (case=10072) 7209, 6972
New! Added option to include or exclude refactoring suggestions in listboxes; see Advanced | Refactoring | Include refactoring suggestions in listboxes. (case=10428) 5797, 7254
"return" takes precedence over "RETCODE" in suggestion lists. (case=11277) 7036
Fixed incompatibility with XNA Game Studio 2.0. (case=10703) 7064
Restored "put n spaces between method name and ()" option under Advanced | Corrections. (case=10995) 7304
Fixed a problem typing "else" on a line by itself. (case=8415)
Debugger tooltips display without flickering. (case=8817) 6647
Fixed hang when opening web site project in Visual Studio 2008. (case=10401)
Fixed intermittent crash when typing a VB Imports statement in Visual Studio 2008 on Windows Vista. (case=15154)
Ctrl+Tab document switching is handled properly in Visual Studio 2008. (case=10303) 7010
Visual Studio 2008 C++ include directories that contain double backslashes due to environment variable expansion are located and parsed correctly. (case=11261)
Highlight current line works properly in .js and .asp/.aspx files in Visual Studio 2008. (case=15384)
Visual Basic enumerations are parsed correctly. (case=12161)
Removed C-style comment and semicolon from Encapsulate Field in Visual Basic files. (case=11160, case=4475) 7107
Object methods called on boxed string literals in C# are parsed correctly. (case=8012) 6513
Subsequent rename operations issued from VA Outline target the correct symbol. (case=10502) 7039
MFC dispatch map is shown in VA Outline. (case=10514)
Tooltips are positioned correctly when VA Outline is undocked in VC6. (case=9976) 6946
VA Outline shows correct hierarchy for C++ classes containing friend methods without friend class declaration. (case=10740) 7072
.Net symbols are excluded from suggestion lists in unmanaged C++ solutions. (case=11391) 7148
Fixed Extract Method with for each loop in managed C++. (case=14864) 7348
Typing a dot after a C++ constructor invocation [ e.g. std::string("some text"). ] displays member list properly. (case=9876)
C++ using declarations (using namespace::identifier) are parsed correctly. (case=9436)
STL list<> and vector<> member lists appear correctly following a "using namespace std::list" or "using namespace std::vector" directive. (case=12345) 7226
Empty C++ preprocessor directives are ignored (fixes problem parsing Boost library). (case=10353)
Typedefs declared inside template classes are parsed correctly. (case=12098)
Improved parsing of STL iterator patterns (case=11842) 7136, 7202, 7212
Improved stability when parsing deep templates. (case=15454)
Fixed dereferenced iterators missing from Find References / Rename. (case=1702) 6451, 4978
Cloned Find References windows are not hidden behind the VC6 IDE. (case=10201) 7000, 7026
IDE warning message about writing to a read-only file is brought to the foreground during a rename operation if applicable. (case=8958)
#include paths are preserved when typing a slash with Repair Case disabled. (case=12025) 7200
C# generic methods are colored properly. (case=9300) 6801
Comments in tooltips and views use IDE comment color. (case=9363) 6904, 6808
Dialog edit controls are colored properly in Windows Vista. (case=9281)
Filenames displayed in lists and views are not syntax colored. (case=12556, case=12788)
Foreground text color is automatically changed if it matches the background color. (case=14268)
VA View Symbols in Solution list items are colored properly. (case=14899)
Quick Info and Parameter Tooltips invoked via the keyboard are colored properly. (case=3082) 5462
Goto (Alt+G) handles template class typedefs. (case=4249) 6168, 5774
Goto handles duplicate files better (as with VSS shared directories). (case=5567, case=7457) 6027, 6021
Listboxes are positioned such that they are not truncated at the right edge of the screen. (case=5178)
Listbox remains on the same side of a symbol (either above or below) when typing inside parentheses. (case=8998) 6672
Windows Vista visual styles are applied to listboxes. (case=9275)
Accepting a suggestion from a listbox properly replaces acronym text. (case=14290) 7316
Fixed disappearing listboxes in .js and .asax files. (case=1699, case=15295) 4843
Fixed bad icons appearing in suggestion lists. (case=14469)
#include directives listed in Find References results are displayed with question mark icons. (case=9592) 6869
Esc key turns off Highlight All when Find References results window is undocked. (case=9364) 6803
Find References error messages may optionally be displayed in the status bar instead of a modal message box. (case=10226) 7038, 6997
Spelling errors in comments with XML tags are underlined. (case=9649) 6893
XML comment suggestion list opened with < functions properly. (case=12324) 7229
Suggestions are suppressed in comments and string literals. (case=10480) 5797, 7030, 7055
Change Signature does not cause some known symbols to become unknown. (case=10432) 6762
"Enable automatic Quick Info Tooltips" option is forced on when enabling "Display comments from source files when available" option in Advanced | Display. (case=10967)
Macros in [VA X install directory]\Misc\stdafx.h are parsed each time a project is opened. (case=284)
Surround with { indents properly when selection is made from bottom to top. (case=1521) 6792, 5961, 5796, 5249
Long words (>64 chars) in comments are not underlined as mistyped. (case=3485) 5634, 5588
Improved Autosave performance. (case=9821) 6916, 5613
Open File in Solution grid lines are drawn correctly in Windows XP. (case=9948)
Forward-declared stable symbols are shown in italic. (case=399) 2263, 1934, 1912
Repair Case changes only those symbols that differ by case alone (similarly named symbols are not changed). (case=8723)
Corrected spacing around spell check replacements. (case=7768) 6471
Eliminated spurious suggestions. (case=12061) 6900
Source files residing in directories named p4win or p4v (or their descendants) are included when parsing. (case=11506) 7150
Missing VC6 toolbar icons have been restored (case=10126) 6980
Filtering toolbar shows the rightmost button correctly. (case=10623) 7044

中文注释有乱码

64,700

社区成员

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

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