26.3. C Library

简介:

26.3.1. lib

26.3.1.1. syslog.h

			
# cat syslog.c
#include <stdio.h>
#include <unistd.h>
#include <syslog.h>

int main(void) {

 openlog("slog", LOG_PID|LOG_CONS, LOG_USER);
 syslog(LOG_INFO, "A different kind of Hello world ... ");
 closelog();

 return 0;
}
			
			
[root@dev1 test]# gcc syslog.c
[root@dev1 test]# ls
a.out  syslog.c
[root@dev1 test]# ./a.out

[root@dev1 test]# tail /var/log/messages
Jan 11 23:52:27 dev1 slog[5056]: A different kind of Hello world ...
			

26.3.1.2. stdio.h

fscanf/fprintf
			
#include<stdio.h>
int main()
{
	FILE *file;
	char name[20][20];

	int a[10]={0};

	int i,j;
	if((file=fopen("1.txt","rt"))==NULL)
	{
		printf("Cannot open file strike any key exit!");
		return 0;
	}
	i=0;
	while(fscanf(file,"%s %d\n",name[i],&a[i])!=EOF)
	{
		i++;
	}
	i=0;
	while(a[i]!=0)
	{
		printf("%s %d\n",name[i],a[i]);
		i++;
	}
	fclose(file);
}
			
			
			
#include <stdio.h>
#include <string.h>

typedef struct _Address
{
	char *name;
	int age;
}Address;

int main()
{
	FILE *file;
	int num;
	char str[256];
	int i;
	char *p;
	Address addr[10]={0};

	if((file=fopen("1.txt","rt"))==NULL)
	{
		printf("Cannot open file strike any key exit!");
		return 0;
	}
	i=0;

	while(fscanf(file,"%s %d\n",str,&num)!=EOF)
	{
		asprintf(&addr[i].name, "%s", str);
		addr[i].age = num;
		i++;
	}
	fclose(file);
	addr[i].name = NULL;

	i=0;
	while(1){
		if(addr[i].name == NULL) break;
		printf("%d: %s %d\n",i, addr[i].name,addr[i].age);
		i++;
	}
}
			
			

26.3.2. libssh2

http://www.libssh2.org/

26.3.3. libconfig – C/C++ Configuration File Library

http://www.hyperrealm.com/main.php?s=libconfig

libconfig 可用于处理 *.conf 配置文件

26.3.4. libuv

提供Socket,进程线程处理等等

http://nikhilm.github.io/uvbook/

26.3.5. newt

https://fedorahosted.org/newt/

http://sourcecodebrowser.com/newt/0.52.10/windows_8c.html

http://gnewt.sourceforge.net/tutorial-4.html#ss4.1

26.3.6. Spdylay - SPDY C Library

http://spdylay.sourceforge.net/

26.3.7. libPhenom

http://facebook.github.io/libphenom/

libPhenom is an eventing framework for building high performance and high scalability systems in C

26.3.8. curl

	
#include <stdio.h>
#include <curl/curl.h>

int main(void)
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
    /* example.com is redirected, so we tell libcurl to follow redirection */
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

    /* Perform the request, res will get the return code */
    res = curl_easy_perform(curl);
    /* Check for errors */
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));

    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  return 0;
}
	
	

get content

	
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

struct string {
  char *ptr;
  size_t len;
};

void init_string(struct string *s) {
  s->len = 0;
  s->ptr = malloc(s->len+1);
  if (s->ptr == NULL) {
    fprintf(stderr, "malloc() failed\n");
    exit(EXIT_FAILURE);
  }
  s->ptr[0] = '\0';
}

size_t writefunc(void *ptr, size_t size, size_t nmemb, struct string *s)
{
  size_t new_len = s->len + size*nmemb;
  s->ptr = realloc(s->ptr, new_len+1);
  if (s->ptr == NULL) {
    fprintf(stderr, "realloc() failed\n");
    exit(EXIT_FAILURE);
  }
  memcpy(s->ptr+s->len, ptr, size*nmemb);
  s->ptr[new_len] = '\0';
  s->len = new_len;

  return size*nmemb;
}

int main(void)
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl) {
    struct string s;
    init_string(&s);

    curl_easy_setopt(curl, CURLOPT_URL, "curl.haxx.se");
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
    res = curl_easy_perform(curl);

    printf("%s\n", s.ptr);
    free(s.ptr);

    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  return 0;
}
	
	

26.3.8.1. url encode / decode

encode

char *subject= curl_easy_escape(curl, "测试主题", 0);		
		

decode

char *subject= curl_easy_unescape(curl, "测试主题", 0);		
		

26.3.9. libxml

26.3.9.1. example

创建xml

		
#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>

int main(int argc, char **argv)
{
	xmlDocPtr doc = NULL;
	xmlNodePtr root_node = NULL, node = NULL, node1 = NULL;

	doc = xmlNewDoc(BAD_CAST "1.0"); // create a new xml document.
	root_node = xmlNewNode(NULL, BAD_CAST "root"); // create a root node.
	xmlDocSetRootElement(doc, root_node);

	xmlNewChild(root_node, NULL, BAD_CAST "node1", BAD_CAST "content of node1");
	//xmlNewChild(root_node, NULL, BAD_CAST "node2", NULL);

	node = xmlNewChild(root_node, NULL, BAD_CAST "node3", BAD_CAST "node3 has attributes");
	xmlNewProp(node, BAD_CAST "attribute", BAD_CAST "yes");

	node = xmlNewNode(NULL, BAD_CAST "node4");
	node1 = xmlNewText(BAD_CAST
	           "other way to create content (which is also a node)");
	xmlAddChild(node, node1);
	xmlAddChild(root_node, node);

	xmlSaveFormatFileEnc(argc > 1 ? argv[1] : "-", doc, "UTF-8", 1);

	xmlFreeDoc(doc);

	xmlCleanupParser();

	xmlMemoryDump();
	return(0);
}
		
		

26.3.9.2. Creating string with libxml2

		
	xmlChar *s;
	int size;
	xmlDocDumpMemory(doc, &s, &size);
	xmlFree(s);

	printf("xml: %s", (char *)s);
		





原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。

目录
相关文章
|
4月前
|
Java 开发工具
开发工具系类 之 Cannot determine path to ‘tools.jar‘ library for 17 (D:/Program Files/Java/jdk-17.0.9)
这篇文章讲述了作者在升级JDK至17版本后遇到IDEA无法识别`tools.jar`的问题,并提供了两种解决方法:升级IDEA版本或降低JDK版本,并提供了相关版本的IDEA兼容性信息。
开发工具系类 之 Cannot determine path to ‘tools.jar‘ library for 17 (D:/Program Files/Java/jdk-17.0.9)
|
7月前
|
机器学习/深度学习 算法 算法框架/工具
Library
Library
40 1
|
JavaScript
dyld: Library not loaded:解决办法
dyld: Library not loaded:解决办法
511 1
|
安全 Ubuntu
LD_LIBRARY_PATH It can compromise security
LD_LIBRARY_PATH It can compromise security
102 1
|
Python
ld: library not found for -lssl
ld: library not found for -lssl
78 0
static library libs/libvpx/libvpx.a is not portable!
static library libs/libvpx/libvpx.a is not portable!
392 0
Xcode12.x报错: ld: library not found for -lstdc++.6.0.9
错误信息: ld: library not found for -lstdc++.6.0.9 报错原因:Xcode10移除了动态库libstdc
276 0
|
JavaScript C++