【发布时间】:2011-03-14 22:49:36
【问题描述】:
我有以下几点: 节点ID = abcde.abc.edu_12345 我需要附加下划线和 time() 返回的 10 位值来创建以下内容: node_inst_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.
*/
}
谁能指出错误是什么?提前致谢。
【问题讨论】:
标签: c arrays networking network-programming char