如何在c中的字符串中添加pid_t

时间:2022-01-15 07:19:56

I am experienced in Java but I am very new to C. I am writing this on Ubuntu. Say I have:

我在Java方面很有经验,但我对C很新。我在Ubuntu上写这篇文章。说我有:

char *msg1[1028];
pid_t cpid;
cpid = fork();

msg1[1] = " is the child's process id.";

How can I concatenate msg1[1] in such a way that when I call:

如何以这样一种方式连接msg1 [1]:

printf("Message: %s", msg1[1]);

The process id will be displayed in front of " is the child's process id."?

进程ID将显示在“是孩子的进程ID”前面吗?

I want to store the whole string in msg1[1]. My ultimate goal isn't just to print it.

我想将整个字符串存储在msg1 [1]中。我的最终目标不仅仅是打印它。

1 个解决方案

#1


4  

Easy solution:

printf("Message: %jd is the child's process id.", (intmax_t)cpid);

Not so easy, but also not too complicated solution: use the (non-portable) asprintf function:

不是那么容易,也不是太复杂的解决方案:使用(非便携式)asprintf功能:

asprintf(&msg[1], "%jd is the child's process id.", (intmax_t)cpid);
// check if msg[1] is not NULL, handle error if it is

If your platform doesn't have asprintf, you can use snprintf:

如果您的平台没有asprintf,则可以使用snprintf:

const size_t MSGLEN = sizeof(" is the child's process id.") + 10; // arbitrary
msg[1] = malloc(MSGLEN);
// handle error if msg[1] == NULL
if (snprintf(msg[1], MSGLEN, "%jd is the child's process id.", (intmax_t)cpid)
  > MSGLEN)
    // not enough space to hold the PID; unlikely, but possible,
    // so handle the error

or define asprintf in terms of snprintf. It's not very hard, but you have to understand varargs. asprintf is very useful to have and it should have been in the C standard library ages ago.

或者根据snprintf定义asprintf。这不是很难,但你必须了解varargs。 asprintf非常有用,它应该早在C标准库中。

EDIT: I originally advised casting to long, but this isn't correct since POSIX doesn't guarantee that a pid_t value fits in a long. Use an intmax_t instead (include <stdint.h> to get access to that type).

编辑:我最初建议铸造为长,但这是不正确的,因为POSIX不保证pid_t值适合长。请改用intmax_t(包括 以访问该类型)。

#1


4  

Easy solution:

printf("Message: %jd is the child's process id.", (intmax_t)cpid);

Not so easy, but also not too complicated solution: use the (non-portable) asprintf function:

不是那么容易,也不是太复杂的解决方案:使用(非便携式)asprintf功能:

asprintf(&msg[1], "%jd is the child's process id.", (intmax_t)cpid);
// check if msg[1] is not NULL, handle error if it is

If your platform doesn't have asprintf, you can use snprintf:

如果您的平台没有asprintf,则可以使用snprintf:

const size_t MSGLEN = sizeof(" is the child's process id.") + 10; // arbitrary
msg[1] = malloc(MSGLEN);
// handle error if msg[1] == NULL
if (snprintf(msg[1], MSGLEN, "%jd is the child's process id.", (intmax_t)cpid)
  > MSGLEN)
    // not enough space to hold the PID; unlikely, but possible,
    // so handle the error

or define asprintf in terms of snprintf. It's not very hard, but you have to understand varargs. asprintf is very useful to have and it should have been in the C standard library ages ago.

或者根据snprintf定义asprintf。这不是很难,但你必须了解varargs。 asprintf非常有用,它应该早在C标准库中。

EDIT: I originally advised casting to long, but this isn't correct since POSIX doesn't guarantee that a pid_t value fits in a long. Use an intmax_t instead (include <stdint.h> to get access to that type).

编辑:我最初建议铸造为长,但这是不正确的,因为POSIX不保证pid_t值适合长。请改用intmax_t(包括 以访问该类型)。