网络编程中char数组的问题
我有以下内容: 节点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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里假设字符串的长度正好是 20 个字符 - 但事实并非如此。试试这个:
编辑:如果你想仍然使用 snprintf,简单的方法是:
This assumes that the string is exactly 20 characters long - it isn't. Try this:
EDIT: If you want to still use snprintf, the easy way is:
您已将下划线硬连接到 node_inst_ID 的索引 20 处,但它仅包含 18 个字符和此时的空终止符:
由于您没有覆盖空终止符,因此打印结果时打印输出将在此处停止。不要假设字符串的位置和长度;相反,使用字符串库函数来操作它们。
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:
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.
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 innode_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.