.SUFFIXES: .c .o CC=gcc SRCS=mydaemon.c OBJS=$(SRCS:.c=.o) EXEC=mydaemon all: $(OBJS) $(CC) -o $(EXEC) $(OBJS) @echo '-------------ok--------------' .c.o: $(CC) -g -o $@ -c $< clean: rm -f $(OBJS) rm -f core*//iconv.c
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <iconv.h> int gbk2utf8(char *src, size_t *srclen, char *dest, size_t *destlen) { iconv_t cd = iconv_open("UTF8", "GBK");//源字符串为gbk,目标为utf8 if (cd ==(iconv_t)-1) { printf("open iconv error %s\n", strerror(errno)); return -1; } size_t rc = iconv(cd, &src, srclen, &dest, destlen);//将src字符串转化为目标dest if (rc == (size_t)-1) { printf("iconv error %s\n", strerror(errno)); return -1; } iconv_close(cd); return 0; } int main(int arg, char *args[]) { if (arg < 2) { printf("Usage: xxx yyy"); return -1; } FILE *p = fopen(args[1], "r"); if (p == NULL) { printf("open %s error, %s\n", args[1], strerror(errno)); return -1; } char buf[1024];//定义一个buffer,存放读取到的文件内容 char destbuf[1024];//定义一个buffer,存放转化完字符串后的内容 while(1) { memset(buf, 0, sizeof(buf)); memset(destbuf, 0, sizeof(destbuf)); if (fgets(buf, sizeof(buf), p) == NULL)//读出一行,如果是文件最后一行,循环退出 { break; } size_t srclen = strlen(buf); size_t destlen = sizeof(destbuf); gbk2utf8(buf, &srclen, destbuf, &destlen); printf("%s\n", destbuf); } fclose(p); return 0; }
.SUFFIXES: .c .o CC=gcc SRCS=curses.c OBJS=$(SRCS:.c=.o) EXEC=mydaemon all: $(OBJS) $(CC) -o $(EXEC) $(OBJS) -lcurses @echo '-------------ok--------------' .c.o: $(CC) -g -o $@ -c $< clean: rm -f $(OBJS) rm -f core*
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <curses.h> int main() { initscr();//开始图形模式 noecho();//不回显 char temp[128]; char s[128]; int seq = 0; while (scanf("%s", s)) { if (strcmp(s, "exit") == 0) { break; } clear();//清除屏幕 sprintf(temp, "please input:"); mvaddstr(0, 0, temp);//在指定的坐标打印 int i; for (i=0; i<10; i++) { sprintf(temp, "%s %d %s", s, seq++, s); mvaddstr(i+1, 0, temp); } move(0, 13);//移动光标到指定坐标 refresh();//刷新屏幕,如果不刷新屏幕,那么mvaddstr和move将不生效 } endwin();//结束图形模式 return 0; }
原文:http://blog.csdn.net/waldmer/article/details/44677113