需要有关发送字符串功能的建议

时间:2011-10-30 06:55:22

标签: c

我是c的新手,但我无法弄清楚如何将此字符串发送到该函数。我尝试了几件事,但它告诉我它正在期待什么

    program.c: In function ‘main’:
    program.c:48:87: error: expected identifier or ‘(’ before ‘long’

       char string1[] =
 "This is process %d with ID %ld and parent id %ld\n", i,  (long)getpid(), (long)getppid());

    write(wrfd1,string1, strlen(string1));

有更好的方法吗?谢谢

2 个答案:

答案 0 :(得分:3)

我认为您打算使用sprintf

int length = 100;
char string1[length];
if(sprintf(string1, "This is process %d with ID %ld and parent id %ld\n", i,  (long)getpid(), (long)getppid())) {
   write(wrfd1,string1, length);
}

答案 1 :(得分:1)

第一个问题是:

 (long)getppid())

支架编号不匹配。

另一个是你不能分配这样的字符串:

int a = 100;
char str[] = "A is %d", a;

如上所述,你应该使用'sprintf'来做到这一点。