请考虑以下代码:
#include #include int main(void) { xmlDocPtr doc; xmlChar *s; doc = xmlParseFile("https://localhost:8000/sitemap.xml"); s = xmlNodeGetContent((struct _xmlNode *)doc); printf("%sn", s); return 0; }
输出:
$ gcc -g3 -O0 $(xml2-config --cflags --libs) 1.c $ ./a.out error : Operation in progress
也就是说, xmlParseFile
会产生不需要的输出。 这里发生的是libxml2
尝试将 localhost
转换为IP地址。 得到的是::1
和127.0.0.1
。 connect("[::1]:8000")
导致EINPROGRESS
(因为libxml2
在描述符上设置了O_NONBLOCK
)。 所以libxml2
等待它完成 ,这导致POLLOUT | POLLERR | POLLHUP
POLLOUT | POLLERR | POLLHUP
POLLOUT | POLLERR | POLLHUP
和libxml2
报告错误 。
后续connect("127.0.0.1:8000")
调用成功,因此所有程序都成功完成。
有没有办法避免这种额外的输出?
正如nwellnhof所建议的那样,可以通过设置error handling程序来规避连接错误。 特别是结构化error handling程序,无论这意味着什
虽然其他问题的答案或多或少地回答了我的问题,但另一个问题是解析器错误。 答案没有提供示例代码。 所以,
#include #include void structuredErrorFunc(void *userData, xmlErrorPtr error) { printf("xmlStructuredErrorFuncn"); } void genericErrorFunc(void *ctx, const char * msg, ...) { printf("xmlGenericErrorFuncn"); } int main(void) { xmlDocPtr doc; xmlChar *s; xmlSetGenericErrorFunc(NULL, genericErrorFunc); xmlSetStructuredErrorFunc(NULL, structuredErrorFunc); doc = xmlParseFile("https://localhost:8000/sitemap.xml"); s = xmlNodeGetContent((struct _xmlNode *)doc); printf("%sn", s); return 0; }
这个输出,
xmlStructuredErrorFunc
以上就是c/c++开发分享如何使libxml2不显示连接错误?相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/523045.html