gpt4 book ai didi

c - c中unix_error函数的问题

转载 作者:行者123 更新时间:2023-11-30 17:35:15 25 4
gpt4 key购买 nike

所以我在使用 unix_error 函数时遇到了问题,我相信我没有包含特定的文件,但我似乎无法在互联网上找到我需要包含的文件。有什么建议吗?

编辑:我写了这样的东西..

while((pid = waitpid(-1, NULL, 0)) > 0){
printf("SERVER: Handler reaped child %d\n", (int) pid);
child_count--;
}
if(errno != ECHILD){
unix_error("waitpid error");
}
sleep(2);
return;

尝试编译 unix_error 时出现错误

最佳答案

没有标准函数unix_error()

出于多种目的,您可以使用此代码。

unixerr.h

#ifndef UNIXERR_H_INCLUDED
#define UNIXERR_H_INCLUDED

extern void unix_error(const char *msg);

#endif /* UNIXERR_H_INCLUDED */

unixerr.c

#include "unixerr.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void unix_error(const char *msg)
{
int errnum = errno;
fprintf(stderr, "%s (%d: %s)\n", msg, errnum, strerror(errnum));
exit(EXIT_FAILURE);
}

还有很多可以改进的地方。第一个方法是将程序名称添加到输出的开头。另一种方法是让函数支持可变长度参数列表,就像 printf() 那样。有些人不喜欢打印错误号;有些人不喜欢打印错误号。我更喜欢它。

关于c - c中unix_error函数的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23035413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com