wcscpy附加了VS2010中以前的结构入口 [英] wcscpy appends previous enteries of structure in VS2010

查看:80
本文介绍了wcscpy附加了VS2010中以前的结构入口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我创建了如下数据结构:-

 typedef  struct 实体{
     int  ID;
     int 类型;
     int 大小;
     int  ParentId;
     int 位置;
     int 长度;
     int  NeteorkTraffic;
    CxyPoint位置;
    wchar_t System [ 1 ];
    wchar_t ConnectionList [ 20 ];
    wchar_t Caption [ 20 ];
} stEntity; 



当我使用以下代码时:-

 CString strData;
pComboBox1-> GetWindowTextW(strData.GetBuffer( 10 ), 10 );
wcscpy(objEntity.System,L " );
pEdit =(CEdit *)GetDlgItem(IDC_EDIT_CONNECTION);
pEdit-> GetWindowTextW(strData);
//  objEntity.System的以下行值再次自动更新为错误值. 
wcscpy(objEntity.ConnectionList,strData); 



它还会在System变量后附加ConnectionList值.
将Cstring复制到wchar_t的操作不正确.

请帮助我解决这个问题.

解决方案

您的System成员只有一个字符宽,因此没有空间可以终止NULL ...所有的字符串函数都使用null来确定它们正在处理的字符串的结尾-在这种情况下,它们会在ConnectionList


中找到第一个null做类似...

 MessageBox(NULL,& system,L "  ,MB_OK); 


您得到System中显示为ConnectionList内容的内容吗?

如果是这种情况,是因为System碰到了ConnectionList.当MessageBox(或使用System的任何对象)期望System是一个以NULL终止的字符串,而不是它时,它只是一个字符,并且内存中的下一个终止​​符在ConnectionList的末尾.

所以你对此能做些什么?第一件事是停止使用期望以NULL终止的字符串操纵东西的函数.它永远不会与您的结构一起工作.其次,停止使用wscpy,即使C程序员也避免使用它,因为它很容易弄乱并且会占用您不应该使用的所有内存.而且,如果您确实需要调用一个期望为System缓冲区提供恒定的零终止字符串的函数,则该函数:

 std :: wstring window_text_buffer( 1 ,objEntity);
MessageBox(NULL,text_buffer.c_str()," ,MB_OK);  
最后的事情是开始成为一名C ++程序员.我知道这听起来很刺耳,但是每次您都在使用C函数时,却并非真正地投入其中.否则您将陷入1990年代初的扭曲之中.停止使用没有任何行为的结构(有时您可能需要数据聚合,但是这种情况并不常见),调查与C函数有关的C ++等效操作,并停止使用C样式


maby(strData)字符串长度大于(jEntity.ConnectionList)长度,开始显示MessageBox中的所有缓冲区和字符串以查看显示内容,然后执行此操作,

Hi,
I have created a data structure as follows : -

typedef struct Entity {
    int Id;
    int Type;
    int Size;
    int ParentId;
    int Position;
    int Length;
    int NeteorkTraffic;
    CxyPoint Location;
    wchar_t System[1];
    wchar_t ConnectionList[20];
    wchar_t Caption[20];
}stEntity;



When i use the following code : -

CString strData;
pComboBox1->GetWindowTextW(strData.GetBuffer(10),10);
wcscpy(objEntity.System,L"A");
pEdit = (CEdit*)GetDlgItem(IDC_EDIT_CONNECTION);
pEdit->GetWindowTextW(strData);
// On Following Line Value Of objEntity.System again updated with wrong value automatically. 
wcscpy(objEntity.ConnectionList,strData);



It also appends the System variable with ConnectionList values.
Copy Of Cstring to wchar_t is not done correctly.

Please help me to slove this issue.

解决方案

your System member is only one char wide, so there''s no room for the terminating NULL ... all the string functions use the presence of a null to determine the end of the string they''re working on - in this case they find the first null in ConnectionList


So when you do something like...

MessageBox( NULL, &system, L"The system is", MB_OK );


you get what''s in System displayed with the contents of ConnectionList?

If that''s the case it''s because System runs into ConnectionList. When MessageBox (or whatever uses System) expects System to be a NULL terminated string and it''s not, it''s just one character and the next terminator in memory is at the end of ConnectionList.

So what can you do about it? First thing is stop using functions that expect NULL terminated strings to manipulate stuff. It''s never going to work with your structure. Secondly stop using wscpy, even C programmers avoid it as it''s so easy to mess up with and go stomping all over memory you shouldn''t. And if you really have to call a function that expects a constant zero terminated string for System buffer it:

std::wstring window_text_buffer( 1, objEntity );
MessageBox( NULL, text_buffer.c_str(), "The system is", MB_OK );


Final thing is start being a C++ programmer. I know this sounds harsh but all the time you''re using C functions and casts your heart''s not really in it. Or you''re stuck in a early 1990s timwarp. Stop using structures that don''t have any behaviour (occasionally you might have need of a data aggregate, but it''s not often), investigate the C++ equivalents of what you''re doing with C functions and stop using C style casts.


maby ( strData ) String length is bigest than ( jEntity.ConnectionList ) length , begining show all buffers and strings in MessageBox to see what show and then do this works ,


这篇关于wcscpy附加了VS2010中以前的结构入口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆