前天写一个小程序, 突然发现其实偶的C学的不怎么样啊,好多函数都不记得,在网上搜到的都是些乱七八糟的,没有原型,用起来不放心,用E文搜,搜到真正的Bible——
The GNU C Library Manual
这里面太全了,只是有一点不太好,E文的,读起来有点慢,感觉有点浪费时间
用C写了段小程序把一个五笔码文本文档转为SQLite3格式的数据库
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>
#include <sqlite3.h>
int main(int argc, char** argv)
{
sqlite3 *db = NULL;
char *zErrMsg = 0;
int rc = 0;
rc = sqlite3_open("wubi.db",&db);
if( rc )
{
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
}
char *sql, insert[256];
sql = "CREATE TABLE word (cn VARCHAR(3) NOT NULL, first VARCHAR(4) NOT NULL, sec VARCHAR(4), pri SMALLINT DEFAULT 0)";
sqlite3_exec( db, sql, 0, 0, &zErrMsg );
sql = "CREATE TABLE wordgroup (cn VARCHAR(12) NOT NULL, first VARCHAR(4) NOT NULL, ind SMALLINT DEFAULT 0)";
sqlite3_exec( db, sql, 0, 0, &zErrMsg );
regex_t reg ;
regmatch_t pm[1];
char *pattern;
char buf[50];
const size_t nmatch = 1;
//取得正则表达式,并进行编译
pattern = argv[1];
int result = regcomp(®, pattern, REG_EXTENDED);
//逐行读取文件
while( fgets(buf, sizeof(buf), stdin) )
{
int e = strlen(buf);
if ( e > 0 && buf[e-1] == '\n' )
buf[e-1] = 0;
result = regexec( ®, buf, nmatch, pm, REG_NOTBOL );
if ( result == REG_NOMATCH )
continue;
int i = 0;
for ( i = 0; i < nmatch && pm[i].rm_so != -1; ++i )
{
int code = e - pm[i].rm_so;
printf("字符\"%s\"的长度为:%d\n",buf, e);
char *c = NULL, *a = NULL, *b = NULL , *word = NULL ;
word = (char *)malloc( 50*sizeof(char) );
c = (char *)malloc( 50*sizeof(char) );
a = (char *)malloc( 4*sizeof(char) );
b = (char *)malloc( 4*sizeof(char) );
//取一个字符串的子字符,前半部
strncpy( word, buf, pm[i].r