简体   繁体   English

有没有更好的方法来修改结构中的 char 数组? C++

[英]Is there a better way to modify a char array in a struct? C++

I am trying to read in a cstring from a edit control box in MFC, then put it into a char array in a struct, but since I cannot do something like clientPacket->path = convertfuntion(a);我正在尝试从 MFC 中的编辑控制框中读取 cstring,然后将其放入结构中的 char 数组中,但是由于我无法执行诸如clientPacket->path = convertfuntion(a); 之类的操作; I had to create another char array to store the string then store it element by element.我必须创建另一个 char 数组来存储字符串,然后逐个元素地存储它。

That felt like a bandait solution, is there a better way to approach this?这感觉像是一个强盗解决方案,有没有更好的方法来解决这个问题? I'd like to learn how to clean up the code.我想学习如何清理代码。

CString stri;//Read text from edit control box and convert it to std::string
GetDlgItem(IDC_EDIT1)->GetWindowText(stri);
string a;
a = CT2A(stri);
char holder[256];

strcpy_s(holder,a.c_str());
int size = sizeof(holder);

struct packet {
    char caseRadio;
    char path[256];
};
packet* clientPacket = new packet;
for (int t = 0; t < size; t++) {
    clientPacket->path[t] = holder[t] ;
}

EDIT:This is currently what I went with:编辑:这是我目前使用的:

CString stri;//Read text from edit control box and convert it to std::string
GetDlgItem(IDC_EDIT1)->GetWindowText(stri);
string a = CT2A(stri);

struct packet {
    char caseRadio;
    char path[CONSTANT];//#define CONSTANT 256
};
packet* clientPacket = new packet;

a = a.substr(0, sizeof(clientPacket->path) - 1);
strcpy_s(clientPacket->path, a.c_str());

I got a problem where I got "1path" instead of "path", turns out it read in caseRadio='1', fixed it by reading out caseRadio first in the server我遇到了一个问题,我得到“1path”而不是“path”,结果它在 caseRadio='1' 中读取,通过首先在服务器中读取 caseRadio 来修复它

You can copy directly into a user-provided buffer when using the Windows API call GetWindowTextA .使用 Windows API 调用GetWindowTextA时,您可以直接复制到用户提供的缓冲区。 The following illustrates how to do this:以下说明了如何执行此操作:

struct packet {
    char caseRadio;
    char path[512];
} p;

::GetWindowTextA(GetDlgItem(IDC_EDIT1)->GetSafeHwnd(), &p.path[0],
                 static_cast<int>(sizeof(p.path)));

This does an implicit character encoding conversion using the CP_ACP code page.这使用CP_ACP代码页进行隐式字符编码转换。 This is not generally desirable, and you may wish to perform the conversion using a known character encoding (such as CP_UTF8 ).这通常是不可取的,您可能希望使用已知的字符编码(例如CP_UTF8 )执行转换。

I don't see the need to create the intermediate 'holder' char array.我认为不需要创建中间“持有人”字符数组。 I think you can just directly do我想你可以直接做

strcpy(clientPacket->path, a.c_str());

You may want to do this:您可能想要这样做:

a= a.substr(0, sizeof(clientPacket->path)-1);

before the strcpy to avoid buffer overrun depending on whether the edit text is size limited or not.在 strcpy 之前避免缓冲区溢出,具体取决于编辑文本是否有大小限制。

Use the CString.GetBuffer function to get a pointer to the string.使用CString.GetBuffer function 获取指向字符串的指针。 In your struct, store the path as a char* instead of a char array.在您的结构中,将路径存储为char*而不是 char 数组。

struct packet {
    char caseRadio;
    char* path;
};
packet* clientPacket = new packet;
clientPacket->path = stri.GetBuffer();

Like this, maybe?像这样,也许? strncpy(clientPacket->path, CT2A(stri).c_str(), 255); . . Also, better make the 256 bytes a constant and use that name, just in case you change this in 10 years.此外,最好将 256 字节设为常量并使用该名称,以防万一您在 10 年后更改此名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM