# 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 ...
#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++; } }
http://www.hyperrealm.com/main.php?s=libconfig
libconfig 可用於處理 *.conf 配置檔案
https://fedorahosted.org/newt/
http://sourcecodebrowser.com/newt/0.52.10/windows_8c.html
http://gnewt.sourceforge.net/tutorial-4.html#ss4.1
http://facebook.github.io/libphenom/
libPhenom is an eventing framework for building high performance and high scalability systems in C
#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; }
創建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); }