网络编程中char数组的问题

发布于 2024-10-22 21:11:04 字数 1345 浏览 2 评论 0原文

我有以下内容: 节点ID = abcde.abc.edu_12345 我需要附加下划线和 time() 返回的 10 位值来创建以下内容: 节点安装 ID = abcde.abc.edu_12345_1016320007 其中 1016320007 是 time() 返回的 10 位数字值。我正在尝试以下代码,但它似乎不起作用:

#define NODE_ID_LENGTH 20
#define NODE_INST_ID 31
char nodeID[NODE_ID_LENGTH]
char node_inst_ID[NODE_INST_ID];
int main()
{
    /*
    Building of nodeID is a bit complex. So its hard to put all the code here. But printing the following at this point gives this
          for(int i = 0; i < NODE_ID_LENGTH; i++)
            {
              printf("%c", nodeID[i]);
            }
    gives
    abcde.abc.edu_12345
    If you need to know any more info about nodeID, I can post the print out of things that you suggest. 
    */

    long int seconds = time(NULL);
    char buf[10];
    sprintf(buf, "%ld", seconds);
    snprintf(&node_inst_ID[0], 20, "%s", &nodeID[0]);
    node_inst_ID[20] = '_';
    node_inst_ID[21] = '\0';
    cout<<"Node instance ID = "<<node_inst_ID<<endl;

    /* 
    I havent yet added the code for appending the time stamp. But I am expecting a print out for the above code like this:
    Node instance ID = abcde.abc.edu_12345_
    But the code is printing only
    Node instance ID = abcde.abc.edu_12345
    It is not adding the underscore at the end. 
    */
}

任何人都可以指出错误是什么吗?提前致谢。

I have the following:
nodeID = abcde.abc.edu_12345
and I need to append the an underscore and a 10digit value returned by time() to create the following:
node_inst_ID = abcde.abc.edu_12345_1016320007
where 1016320007 is the 10 digit value returned by time(). I am trying the following code but it doesnt seem to work:

#define NODE_ID_LENGTH 20
#define NODE_INST_ID 31
char nodeID[NODE_ID_LENGTH]
char node_inst_ID[NODE_INST_ID];
int main()
{
    /*
    Building of nodeID is a bit complex. So its hard to put all the code here. But printing the following at this point gives this
          for(int i = 0; i < NODE_ID_LENGTH; i++)
            {
              printf("%c", nodeID[i]);
            }
    gives
    abcde.abc.edu_12345
    If you need to know any more info about nodeID, I can post the print out of things that you suggest. 
    */

    long int seconds = time(NULL);
    char buf[10];
    sprintf(buf, "%ld", seconds);
    snprintf(&node_inst_ID[0], 20, "%s", &nodeID[0]);
    node_inst_ID[20] = '_';
    node_inst_ID[21] = '\0';
    cout<<"Node instance ID = "<<node_inst_ID<<endl;

    /* 
    I havent yet added the code for appending the time stamp. But I am expecting a print out for the above code like this:
    Node instance ID = abcde.abc.edu_12345_
    But the code is printing only
    Node instance ID = abcde.abc.edu_12345
    It is not adding the underscore at the end. 
    */
}

Can anyone point out what the mistake is? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

任谁 2024-10-29 21:11:04
snprintf(&node_inst_ID[0], 20, "%s", &nodeID[0]);
node_inst_ID[20] = '_';
node_inst_ID[21] = '\0';

这里假设字符串的长度正好是 20 个字符 - 但事实并非如此。试试这个:

snprintf(&node_inst_ID[0], 20, "%s", &nodeID[0]);
strcat(node_inst_ID, "_"); // strcat is safe in this context, usually you should use strncat.

编辑:如果你想仍然使用 snprintf,简单的方法是:

int len = strlen(node_inst_ID);
snprintf(node_inst_ID + len, sizeof(node_inst_ID) - len, "%whatever", the, args);
snprintf(&node_inst_ID[0], 20, "%s", &nodeID[0]);
node_inst_ID[20] = '_';
node_inst_ID[21] = '\0';

This assumes that the string is exactly 20 characters long - it isn't. Try this:

snprintf(&node_inst_ID[0], 20, "%s", &nodeID[0]);
strcat(node_inst_ID, "_"); // strcat is safe in this context, usually you should use strncat.

EDIT: If you want to still use snprintf, the easy way is:

int len = strlen(node_inst_ID);
snprintf(node_inst_ID + len, sizeof(node_inst_ID) - len, "%whatever", the, args);
昵称有卵用 2024-10-29 21:11:04

您已将下划线硬连接到 node_inst_ID 的索引 20 处,但它仅包含 18 个字符和此时的空终止符:

0123456789012345678
abcde.abc.edu_12345

由于您没有覆盖空终止符,因此打印结果时打印输出将在此处停止。不要假设字符串的位置和长度;相反,使用字符串库函数来操作它们。

You've hardwired the underscore to be at index 20 of node_inst_ID, however it only contains 18 characters and the null terminator at that point:

0123456789012345678
abcde.abc.edu_12345

Since you are not overwriting the null terminator, that's where the printout stops when you print the result. Don't assume positions and lengths for strings; instead, use string library functions to manipulate them.

香草可樂 2024-10-29 21:11:04

nodeID 只有 19 个字符长。因此它在位置 19 处有一个 \0 终止符,您在 node_inst_ID 中创建的副本也是如此。然后,您从位置 20 开始存储更多内容,在终止零字节之后。所以你的下划线位于字符串末尾之后。

nodeID is only 19 characters long. So it has a \0 terminator in position 19, and so does the copy you make in node_inst_ID. Then you store some more stuff starting at position 20, after the terminating zero-byte. So your underscore is after the end of the string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文