发个从文件中读取身份证号15位升18位的小程序(如果不是唯一字段 还有待完善 请大神指点)
以下代码只是模拟从文本文件中读取每行起始处为15位的身份证号如果文件格式为其它样式则不成立
只为抛砖引玉
希望看到能从dbf3以上版本文件中读取某个字段的15位(自动排除18位)身份证号
并升级成18位身份证号后写入原始dbf文件的C代码
程序代码:
#include <stdio.h> #include <stdlib.h> int readNum(int *n, FILE *fp); void newId(char *id, int *n); int calc(int *n); void writeFile(FILE *fn, char *id); int main(int argc, char *argv[]) { int n[17] = {0, 0, 0, 0, 0, 0, 1, 9,}; const char s[11] = "10X98765432"; char id[18]; FILE *fp, *fn; if(argc != 3) exit(EXIT_FAILURE); fp = fopen(argv[1], "r"); fn = fopen(argv[2], "w+"); if(!fp || !fn) exit(EXIT_FAILURE); else { while(!feof(fp)) { if(readNum(n, fp) == EOF) break; newId(id, n); id[17] = s[calc(n)]; writeFile(fn, id); } } fclose(fp); fclose(fn); return 0; } int readNum(int *n, FILE *fp) { int i, ch; for(i = 0; i < 6; i++) fscanf(fp, "%1d", n + i); for(i = 8; i < 17; i++) fscanf(fp, "%1d", n + i); while((ch = getc(fp)) != '\n' && ch != EOF); return ch; } void newId(char *id, int *n) { int i = -1; while(++i < 17) id[i] = n[i] + '0'; } int calc(int *n) { int i = -1, s = 0; const int m[17] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}; while(++i < 17) s += n[i] * m[i]; return s % 11; } void writeFile(FILE *fn, char *id) { int i = -1; while(++i < 18) putc(id[i], fn); putc('\n', fn); }