用c语言写了个解析文件并写入到数据库,求大师指导!

C语言 码拜 9年前 (2015-05-11) 1149次浏览 0个评论

前言:
本人不是c开发,对大学的c也忘得差不多了,突然有个需求要用c写,简直急死个人啊,网上查资料拼了代码,求指导!需求如下:
1 打开文件 
获取文件夹下匹配成功的文件(昨天的文件)
2 获取内容,解析文件,判断普通用户/智能用户
普通用户字段6个,智能电表用户8个字段,全部转化成8个字段(拆分,合并)
3 打开数据库,将对账文件中的数据写入数据库
oracle 数据库 插入数据
4 关闭文件,关闭数据库
关闭数据库关闭文件

代码如下:

#include <time.h>
#include <stdio.h>
#define COL 10 // 定义文本中每一行的数据的字段的数量
#define BUFSIZE 1024 // 缓冲区域大小
#define FILEDIR “$HOME/file/dsdf/dzxx/”

int main() {
FILE *fp; /*文件指针*/
char *filePath;/*文件地址*/
char buf[1024];/*单行数据缓存*/

// 
char *sysDate = getSysDate();

// 
getFiles();
return 0;
}

/*获取系统前一天日期*/
char getSysDate(char *sysDate) {
SYSTEMTIME st;
GetSystemTime(&st);
char *data = new char[8];// 系统时间
sprintf(data,”%d%d%d”,st.wYear, st.wMonth, st.wDay-1);
}

/*获取指定文件夹下的指定格式的文件(E或NE开头的时间为前一天的文件)*/
void getFiles() {
char *filedir = “”””
char ch,infile[50],outfile[50],filename[20];
    struct dirent *ptr;    
    DIR *dir;
    dir=opendir(filedir);
    while((ptr=readdir(dir))!=NULL)     {
     if(ptr->d_name[0] == “”.”” || ptr->d_name[0] == “”tmp””)
       continue;
      sprintf(filename,”%s”,ptr->d_name);
      sprintf(infile,”%s%s”,FILEDIR,ptr->d_name);
      // 取系统昨日的文件解析数据
      if(strstr(filename,sysDate)) {
       parseFileToSQL(infile)
      }
    }
    closedir(dir);
}

/*解析文件并写入数据库*/
int parseFileToSQL(char filePath[]) {
FILE *fp; /*文件指针*/

char buf[BUFSIZE];/*单行数据缓存*/
char sql[BUFSIZE];

char yhbh[4];/*银行编号*/
char gyh[8];/*柜员号*/
char yhlsh[6];/*银行流水号*/
char yhbh_1[10];/*用户编号*/
char jfrq[8];/*缴费日期*/
double sfje[20];/*收费金额*/
char sfqd[3];/*收费渠道*/
int isznbsf[1];/*是否是智能表收费*/

char temp[1024];// 

if((fp = fopen(filePath, “r”)) == NULL) {
            printf(“fopen error\n”);
            
            
            return 0;
  }
   // 连接数据库并插入数据
EXEC SQL CONNECT GAPS/GAPS;

// 判断文件名是以E开头还是NE开头的
// 将E开头的文件中的数据拆分与NE开头的文件数据相同的字段

char *ne = “NE”;
if(strstr(fp,ne) != NULL) {
  while(fgets(buf, BUFFSIZE-2, file)) {
   buf = copy(buf,2,BUFSIZE-1);// 去首字符
            buf = copy(buf,1,BUFSIZE-2);// 去尾字符
   // 获取按照“|”分割后的数据
          //printf(buf);
          yhbh = strtok(buf,”|”);
          gyh=strtok(NULL,”|”);
          yhlsh=strtok(NULL,”|”);
          yhbh=strtok(NULL,”|”);
          jfrq=strtok(NULL,”|”);
          sfje=strtok(NULL,”|”);
          sfqd=strtok(NULL,”|”);
          isznbsf=strtok(NULL,”|”);
          
fprintf(sql,”insert into yw_dsdf_dzxx_hb values(“”%s””,””%s””,””%s””,””%s””,””%s””,””%d””,””%s””,””%d””)”,yhbh,gyh,yhlsh,yhbh_1,jfrq,sfje,sfqd,isznbsf);

sql_BeginTrans();
EXEC SQL INSERT INTO yw_dsdf_dzxx_hb values(sql);
sql_CommitTrans();
}
} else {
while(fgets(buf, BUFFSIZE-2, file)) {
buf = copy(buf,2,BUFSIZE-1);// 去首字符
            buf = copy(buf,1,BUFSIZE-2);// 去尾字符
   // 获取按照“|”分割后的数据
          //printf(buf);
          yhbh = strtok(buf,”|”);
          strtok(NULL,”|”);
          gyh = “”;
          temp=strtok(NULL,”|”);
          
          memset(yhlsh,0,sizeof(yhlsh));
          memcpy(yhlsh,temp+12,6);// 流水号
          memset(jfrq,0,sizeof(jfrq));
          memcpy(jfrq,temp+4,8);
          
          yhbh=strtok(NULL,”|”);
          sfje=strtok(NULL,”|”);
          sfqd = “”;
          isznbsf=”0″;
fprintf(sql,”insert into yw_dsdf_dzxx_hb values(“”%s””,””%s””,””%s””,””%s””,””%s””,””%d””,””%s””,””%d””)”,yhbh,gyh,yhlsh,yhbh_1,jfrq,sfje,sfqd,isznbsf);

//sql_BeginTrans();
EXEC SQL INSERT INTO yw_dsdf_dzxx_hb values(sql);
//sql_CommitTrans();
}
}
return 0;
}

100分
获取前一天不能简单地将日期减一。
引用 1 楼 zhao4zhong1 的回复:

获取前一天不能简单地将日期减一。

那要怎么做?

#include <stdio.h>
#include <windows.h>
char *getYesterday() {
    FILETIME ft;
    SYSTEMTIME st;
    static char yesterday[9];

    GetSystemTimeAsFileTime(&ft);
    *(__int64 *)&ft-=864000000000i64;
    FileTimeToSystemTime(&ft,&st);
    sprintf(yesterday,"%4d%02d%02d",st.wYear, st.wMonth, st.wDay);
    return (char *)yesterday;
}
int main() {
    printf("%s\n",getYesterday());
    return 0;
}
引用 3 楼 zhao4zhong1 的回复:
#include <stdio.h>
#include <windows.h>
char *getYesterday() {
    FILETIME ft;
    SYSTEMTIME st;
    static char yesterday[9];

    GetSystemTimeAsFileTime(&ft);
    *(__int64 *)&ft-=864000000000i64;
    FileTimeToSystemTime(&ft,&st);
    sprintf(yesterday,"%4d%02d%02d",st.wYear, st.wMonth, st.wDay);
    return (char *)yesterday;
}
int main() {
    printf("%s\n",getYesterday());
    return 0;
}

如果是在Linux下就不能用了吧,我看这边引了windows.h

在请教下哈 
GetSystemTimeAsFileTime(&ft);
    *(__int64 *)&ft-=864000000000i64;
    FileTimeToSystemTime(&ft,&st);

这个是个什么意思?
引用 4 楼 xiaolubian 的回复:
Quote: 引用 3 楼 zhao4zhong1 的回复:
#include <stdio.h>
#include <windows.h>
char *getYesterday() {
    FILETIME ft;
    SYSTEMTIME st;
    static char yesterday[9];

    GetSystemTimeAsFileTime(&ft);
    *(__int64 *)&ft-=864000000000i64;
    FileTimeToSystemTime(&ft,&st);
    sprintf(yesterday,"%4d%02d%02d",st.wYear, st.wMonth, st.wDay);
    return (char *)yesterday;
}
int main() {
    printf("%s\n",getYesterday());
    return 0;
}

如果是在Linux下就不能用了吧,我看这边引了windows.h

你一楼给的代码里面不是也用了SYSTEMTIME吗?linux下有SYSTEMTIME?

引用 4 楼 xiaolubian 的回复:
Quote: 引用 3 楼 zhao4zhong1 的回复:
#include <stdio.h>
#include <windows.h>
char *getYesterday() {
    FILETIME ft;
    SYSTEMTIME st;
    static char yesterday[9];

    GetSystemTimeAsFileTime(&ft);
    *(__int64 *)&ft-=864000000000i64;
    FileTimeToSystemTime(&ft,&st);
    sprintf(yesterday,"%4d%02d%02d",st.wYear, st.wMonth, st.wDay);
    return (char *)yesterday;
}
int main() {
    printf("%s\n",getYesterday());
    return 0;
}

如果是在Linux下就不能用了吧,我看这边引了windows.h


“跨平台一定比不跨平台好”
就和说
“双性恋一定比异性恋好”
一样!

引用 5 楼 xiaolubian 的回复:

在请教下哈 
GetSystemTimeAsFileTime(&ft);
    *(__int64 *)&ft-=864000000000i64;
    FileTimeToSystemTime(&ft,&st);

这个是个什么意思?

GetSystemTimeAsFileTime(&ft);//取当前系统时间放在ft中
*(__int64 *)&ft-=864000000000i64;//将ft减去86400秒
FileTimeToSystemTime(&ft,&st);//将ft转为st
Quote: 引用 7 楼 zhao4zhong1 的回复:
Quote: 引用 4 楼 xiaolubian 的回复:
Quote: 引用 3 楼 zhao4zhong1 的回复:

[code=c]#include <stdio.h>
#include <windows.h>
char *getYesterday() {
    FILETIME ft;
    SYSTEMTIME st;
    static char yesterday[9];

我新手,感谢指导,自己也在不断编译调试中,现在的问题如下:
DzxxHB.c:31:50: warning: multi-character character constant
DzxxHB.c: In function “”parseFileToSQL””:
DzxxHB.c:68: error: “”EXEC”” undeclared (first use in this function)
DzxxHB.c:68: error: (Each undeclared identifier is reported only once
DzxxHB.c:68: error: for each function it appears in.)
DzxxHB.c:68: error: expected “”;”” before “”SQL””
DzxxHB.c:74: warning: passing argument 1 of “”strstr”” from incompatible pointer type
/usr/include/string.h:342: note: expected “”const char *”” but argument is of type “”struct FILE *””
DzxxHB.c:75: error: “”BUFFSIZE”” undeclared (first use in this function)
DzxxHB.c:75: error: “”file”” undeclared (first use in this function)
DzxxHB.c:76: error: incompatible types when assigning to type “”char[1024]”” from type “”int””
DzxxHB.c:77: error: incompatible types when assigning to type “”char[1024]”” from type “”int””
DzxxHB.c:80: error: incompatible types when assigning to type “”char[4]”” from type “”char *””
DzxxHB.c:81: error: incompatible types when assigning to type “”char[8]”” from type “”char *””
DzxxHB.c:82: error: incompatible types when assigning to type “”char[6]”” from type “”char *””
DzxxHB.c:83: error: incompatible types when assigning to type “”char[4]”” from type “”char *””
DzxxHB.c:84: error: incompatible types when assigning to type “”char[8]”” from type “”char *””
DzxxHB.c:85: error: incompatible types when assigning to type “”double[20]”” from type “”char *””
DzxxHB.c:86: error: incompatible types when assigning to type “”char[3]”” from type “”char *””
DzxxHB.c:87: error: incompatible types when assigning to type “”int[1]”” from type “”char *””
DzxxHB.c:89: warning: passing argument 1 of “”fprintf”” from incompatible pointer type
/usr/include/stdio.h:355: note: expected “”struct FILE * __restrict__”” but argument is of type “”char *””
DzxxHB.c:92: error: expected “”;”” before “”SQL””
DzxxHB.c:97: error: incompatible types when assigning to type “”char[1024]”” from type “”int””
DzxxHB.c:98: error: incompatible types when assigning to type “”char[1024]”” from type “”int””
DzxxHB.c:101: error: incompatible types when assigning to type “”char[4]”” from type “”char *””
DzxxHB.c:103: error: incompatible types when assigning to type “”char[8]”” from type “”char *””
DzxxHB.c:104: error: incompatible types when assigning to type “”char[1024]”” from type “”char *””
DzxxHB.c:111: error: incompatible types when assigning to type “”char[4]”” from type “”char *””
DzxxHB.c:112: error: incompatible types when assigning to type “”double[20]”” from type “”char *””
DzxxHB.c:113: error: incompatible types when assigning to type “”char[3]”” from type “”char *””
DzxxHB.c:114: error: incompatible types when assigning to type “”int[1]”” from type “”char *””
DzxxHB.c:115: warning: passing argument 1 of “”fprintf”” from incompatible pointer type
/usr/include/stdio.h:355: note: expected “”struct FILE * __restrict__”” but argument is of type “”char *””
DzxxHB.c:118: error: expected “”;”” before “”SQL””

对于头文件的引用,不晓得下面这些正确吗?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <time.h>
#include <sqlca.h>
你代码中:
  // 连接数据库并插入数据
EXEC SQL CONNECT GAPS/GAPS;
需要进一步编写函数完成对应功能。
参考oracle中的例子代码demo\*.*
引用 11 楼 zhao4zhong1 的回复:

你代码中:
  // 连接数据库并插入数据
EXEC SQL CONNECT GAPS/GAPS;
需要进一步编写函数完成对应功能。
参考oracle中的例子代码demo\*.*

fprintf(sql,”insert into yw_dsdf_dzxx_hb values(“”%s””,””%s””,””%s””,””%s””,””%s””,””%d””,””%s””,””%d””)”,yhbh,gyh,yhlsh,yhbh_1,jfrq,sfje,sfqd,isznbsf);

EXEC SQL INSERT INTO yw_dsdf_dzxx_hb values(sql);
这个算是吗?写了个insert 函数

参考oracle中的例子代码demo\*.*
引用 13 楼 zhao4zhong1 的回复:

参考oracle中的例子代码demo\*.*

参考了demo例子,还是出现那个问题,在引入sql头文件时,
#include <sqlca.h> 和 EXEC SQL include “sqlca.h” 和 EXEC SQL include sqlca
这三者有什么区别吗?
始终报
DzxxHB.c: In function “”main””:
DzxxHB.c:30: error: “”EXEC”” undeclared (first use in this function)
DzxxHB.c:30: error: (Each undeclared identifier is reported only once
DzxxHB.c:30: error: for each function it appears in.)
DzxxHB.c:30: error: expected “”;”” before “”SQL””
DzxxHB.c:32:50: warning: multi-character character constant
DzxxHB.c: In function “”parseFileToSQL””:
DzxxHB.c:111: error: “”EXEC”” undeclared (first use in this function)
DzxxHB.c:111: error: expected “”;”” before “”SQL””
这个错,崩溃了。

先将demo目录下插入数据的例子编译链接调试通过,再将其中操作数据库相关部分照搬到自己的代码中。
突然回来结个贴。不好意思,问题解决了。
迟来的幸福!

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明用c语言写了个解析文件并写入到数据库,求大师指导!
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!