원본 http://blog.naver.com/julymorning4/100024942961

[예제]텍스트 파일을 읽어 DB에...
/*======================================================*/
/* 프로그램 그룹 : 기준 정보*/
/* 프로그램명:*/
/* 설명: 날씨 정보 텍스트 파일을 읽어 없으면*/
/* Insert있으면 Update*/
/**/
/* 작성일시: 2005.04.XX */
/* 작성자:*/
/* INPUT DATA: 날씨 정보 파일(텍스트) */
/* REF. DATA : None.*/
/* OUTPUT DATA : WEATHER*/
/*======================================================*/
/* 변경 History */
/*======================================================*/
/* 2005.04.07 DEV START */
/**/
/*======================================================*/

#include "../../comm/jclee.h"
#define READ_SIZE83

EXEC SQL include sqlca.h;

/*======================================================*/
/* Global 변수 선언 */
/*======================================================*/
FILE* fi;
chardummy[100];
charInput_Data [READ_SIZE];
charInput_Buf[READ_SIZE];
longgl_job = 0;/* Job Status*/


/*======================================================*/
/* 프로그램 정보(ID, Name)*/
/*======================================================*/
char Pg_Info [2][50];


/*======================================================*/
/* 프로그램 공통 함수 */
/*======================================================*/
void Check_Command(int argc, char *argv[]);
void DataLogging();
intData_Processing();
intFile_Open();
void File_Close();
void Save_DataFile();


/*======================================================*/
/* READ DATA STRUCTURE*/
/*======================================================*/
typedef struct Read_Record {
char Wth_Date [ 8];/* 날씨정보 기준일 */
char Code [ 3];/* 코드*/
char Am_Wth_Code[ 2];/* 오전 날씨 아이콘*/
char Pm_Wth_Code[ 2];/* 오후 날씨 아이콘*/
char Min_Temp [ 3];/* 당일 최저 기온*/
char Max_Temp [ 3];/* 당일 최고 기온*/
char Am_Rain_Rate [ 3];/* 오전 강수 확률*/
char Pm_Rain_Rate [ 3];/* 오후 강수 확률*/
char Min_Humi [ 3];/* 당일 최저 습도*/
char Max_Humi [ 3];/* 당일 최고 습도*/
char Wth_Info [50];/* 날씨 요약 정보*/
} Read_Rec;


/*======================================================*/

/*======================================================*/
/* REAL STRUCTURE */
/*======================================================*/
typedef struct Real_Record {
char Wth_Date[ 8+1];/* 날씨정보 기준일 */
char Code[ 3+1];/* 점코드*/
char Am_Wth_Code [ 2+1];/* 오전 날씨 아이콘*/
char Pm_Wth_Code [ 2+1];/* 오후 날씨 아이콘*/
char Min_Temp[ 3+1];/* 당일 최저 기온*/
char Max_Temp[ 3+1];/* 당일 최고 기온*/
char Am_Rain_Rate[ 3+1];/* 오전 강수 확률*/
char Pm_Rain_Rate[ 3+1];/* 오후 강수 확률*/
char Min_Humi[ 3+1];/* 당일 최저 습도*/
char Max_Humi[ 3+1];/* 당일 최고 습도*/
char Wth_Info[50+1];/* 날씨 요약 정보*/
} Real_Rec;

Read_RecReadRecord;
Real_RecRealRecord ;

/*=======================================================*/
/* */
/* MAIN START......*/
/* */
/*=======================================================*/
void main(int argc, char *argv[])
{
int ret_val;

/* argument check & variable init */
Check_Command(argc, &argv[0]);

DB_Connect(Pg_Info[0]);

ret_val = File_Open();

if ( ret_val != 0 ) {
printf("FILE OPEN Error! \n");
return;
}

/* FILE을 읽어 날씨 정보 테이블(WEATHER)에 저장 */
gl_job = Data_Processing();

if (gl_job != 0) {
DB_RollbackR();
}
else {
Save_DataFile();
DB_CommitR();
}

/* File_Close(); */
File_Close();

Pgm_End(Pg_Info[0], 0);
}

/*=======================================================*/
/* 함수명 : Check_Command*/
/* 기능 : Command Syntax 확인*/
/*=======================================================*/
void Check_Command(int argc, char *argv[]) {

/* MEM(Pg_Info); */
memset(Pg_Info, 0, sizeof(Pg_Info));

strcpy(Pg_Info[0], argv[0]);
strcpy(Pg_Info[1], "날씨 정보 다운로드");

/* argument print */
Log_Arg(argc, &argv[0], Pg_Info[0]);

/* Program Start Log.... */
Log_Line(Pg_Info[0], "====================Loggging Start===================");
Pgm_Start(Pg_Info[0]);
}



/*=====================================================*/
/* 함수명 : Data_Processing*/
/* 기능 : SAM FILE을 날씨 정보 테이블(WEATHER)에 저장*/
/*=====================================================*/
intData_Processing()
{
int pos = 0, col = 0;
int cnt = 0;
int Tot_Cnt = 0;
int Read_Chk = 0;
charCur_Date[9];
int Chk_Cnt;
charLog_Text[1024];


Read_Chk = fread (Input_Data,sizeof(Input_Data) ,1, fi) ;

while ( Read_Chk > 0 )
{

memset(Cur_Date, 0x00, sizeof(Cur_Date));
memset(&ReadRecord, 0x00,sizeof(ReadRecord));
memset(&RealRecord, 0x00, sizeof(RealRecord));

col = 0;

strncpy(ReadRecord.Wth_Date, Input_Data+col, 8); col+=8;
strncpy(ReadRecord.Code, Input_Data+col, 3); col+=3;
strncpy(ReadRecord.Am_Wth_Code,Input_Data+col, 3); col+=2;
strncpy(ReadRecord.Pm_Wth_Code,Input_Data+col, 3); col+=2;
strncpy(ReadRecord.Min_Temp, Input_Data+col, 3); col+=3;
strncpy(ReadRecord.Max_Temp, Input_Data+col, 3); col+=3;
strncpy(ReadRecord.Am_Rain_Rate, Input_Data+col, 3); col+=3;
strncpy(ReadRecord.Pm_Rain_Rate, Input_Data+col, 3); col+=3;
strncpy(ReadRecord.Min_Humi, Input_Data+col, 3); col+=3;
strncpy(ReadRecord.Max_Humi, Input_Data+col, 3); col+=3;
strncpy(ReadRecord.Wth_Info, Input_Data+col, 50);

cnt = cnt + 1 ;

strncpy(RealRecord.Wth_Date,ReadRecord.Wth_Date,sizeof(ReadRecord.Wth_Date));
strncpy(RealRecord.Code ,ReadRecord.Code ,sizeof(ReadRecord.Code ));
strncpy(RealRecord.Am_Wth_Code,ReadRecord.Am_Wth_Code,sizeof(ReadRecord.Am_Wth_Code));
strncpy(RealRecord.Pm_Wth_Code,ReadRecord.Pm_Wth_Code,sizeof(ReadRecord.Pm_Wth_Code));
strncpy(RealRecord.Min_Temp ,ReadRecord.Min_Temp ,sizeof(ReadRecord.Min_Temp ));
strncpy(RealRecord.Max_Temp ,ReadRecord.Max_Temp ,sizeof(ReadRecord.Max_Temp ));
strncpy(RealRecord.Am_Rain_Rate ,ReadRecord.Am_Rain_Rate ,sizeof(ReadRecord.Am_Rain_Rate ));
strncpy(RealRecord.Pm_Rain_Rate ,ReadRecord.Pm_Rain_Rate ,sizeof(ReadRecord.Pm_Rain_Rate ));
strncpy(RealRecord.Min_Humi ,ReadRecord.Min_Humi ,sizeof(ReadRecord.Min_Humi ));
strncpy(RealRecord.Max_Humi ,ReadRecord.Max_Humi ,sizeof(ReadRecord.Max_Humi ));
strncpy(RealRecord.Wth_Info ,ReadRecord.Wth_Info ,sizeof(ReadRecord.Wth_Info ));

printf("Read Data Stream => [%s] \n",Input_Data);

printf("Read Data Check1 => [%s][%s][%s][%s][%s][%s][%s][%s][%s][%s]\n",
RealRecord.Wth_Date,RealRecord.Code,
RealRecord.Am_Wth_Code, RealRecord.Pm_Wth_Code,
RealRecord.Am_Rain_Rate, RealRecord.Pm_Rain_Rate,
RealRecord.Max_Temp,RealRecord.Min_Temp,
RealRecord.Max_Humi,RealRecord.Min_Humi
);
printf("Read Data Check2 => [%s] \n\n",RealRecord.Wth_Info);

/*-------------------------------------------------------------*/
EXEC SQL
SELECT COUNT(WTH_DATE)
INTO :Chk_Cnt
FROM WEATHER
WHEREWTH_DATE = :RealRecord.Wth_Date
ANDCode = SUBSTR(:RealRecord.Code,2,2);

/* ------------------------------------------------------------*/
/* 기존 데이터가 없는 경우 */
/* ------------------------------------------------------------*/
if (Chk_Cnt == 0) {
/* 단기 3일치엔 습도가 제공 되지만 주간예보 5일치엔 습도가 제공안됨 */
if (strcmp(RealRecord.Max_Humi, " ") == 0) {
EXEC SQL
INSERT INTO WEATHER (
Code, /* SAM FILE에서 넘어오는 점코드 */
WTH_DATE, /* 일자 */
AM_WTH_CODE,/* 오전 날씨*/
PM_WTH_CODE,/* 오후 날씨*/
AM_RAIN_RATE, /* 오전 비(or SNOW)올 확률*/
PM_RAIN_RATE, /* 오후 비(or SNOW)올 확률*/
MIN_TEMP, /* 최저 기온*/
MAX_TEMP, /* 최고 기온*/
WTH_INFO/* 개황 */
)
VALUES (
SUBSTR(:RealRecord.Code,2,2),
:RealRecord.Wth_Date,
:RealRecord.Am_Wth_Code,
:RealRecord.Pm_Wth_Code,
TO_NUMBER(:RealRecord.Am_Rain_Rate),
TO_NUMBER(:RealRecord.Pm_Rain_Rate),
TO_NUMBER(:RealRecord.Min_Temp),
TO_NUMBER(:RealRecord.Max_Temp),
:RealRecord.Wth_Info
);

}
else {
EXEC SQL
INSERT INTO WEATHER (
Code, /* SAM FILE에서 넘어오는 점코드 */
WTH_DATE, /* 일자 */
AM_WTH_CODE,/* 오전 날씨*/
PM_WTH_CODE,/* 오후 날씨*/
AM_RAIN_RATE, /* 오전 비(or SNOW)올 확률*/
PM_RAIN_RATE, /* 오후 비(or SNOW)올 확률*/
MIN_TEMP, /* 최저 기온*/
MAX_TEMP, /* 최고 기온*/
MIN_HUMI, /* 최저 습도*/
MAX_HUMI, /* 최고 습도*/
WTH_INFO/* 개황 */
)
VALUES (
SUBSTR(:RealRecord.Code,2,2),
:RealRecord.Wth_Date,
:RealRecord.Am_Wth_Code,
:RealRecord.Pm_Wth_Code,
TO_NUMBER(:RealRecord.Am_Rain_Rate),
TO_NUMBER(:RealRecord.Pm_Rain_Rate),
TO_NUMBER(:RealRecord.Min_Temp),
TO_NUMBER(:RealRecord.Max_Temp),
TO_NUMBER(:RealRecord.Min_Humi),
TO_NUMBER(:RealRecord.Max_Humi),
:RealRecord.Wth_Info
);
}

if (SQLCODE != SQL_OK) {
DB_Error(&Pg_Info[0]);
printf("\n####### 날씨 Insert Error Data(WEATHER) #######\n");
printf("\n[%d]\n[%s]\n",SQLCODE,SQLERRTXT);
Log_Line(Pg_Info[0],"####### 날씨 Insert Error Data(WEATHER) #######\n");
DataLogging();
return -1;
}
else {
printf("####### Insert OK #######\n\n");
}
}
/*-----------------------------------------------*/
/* 기존 데이터가 있는 경우 */
/*-----------------------------------------------*/
else {
/* 단기 3일치엔 습도가 제공 되지만 주간예보 5일치엔 습도가 제공안됨 */
if (strcmp(RealRecord.Max_Humi, " ") == 0) {
EXEC SQL
UPDATE WEATHER
SETAM_WTH_CODE= :RealRecord.Am_Wth_Code,
PM_WTH_CODE= :RealRecord.Pm_Wth_Code,
AM_RAIN_RATE = TO_NUMBER(:RealRecord.Am_Rain_Rate),
PM_RAIN_RATE = TO_NUMBER(:RealRecord.Pm_Rain_Rate),
MIN_TEMP = TO_NUMBER(:RealRecord.Min_Temp),
MAX_TEMP = TO_NUMBER(:RealRecord.Max_Temp),
WTH_INFO = RTRIM(:RealRecord.Wth_Info)
WHEREWTH_DATE = :RealRecord.Wth_Date
ANDCode = SUBSTR(:RealRecord.Code, 2,2);

}
else {
EXEC SQL
UPDATE WEATHER
SETAM_WTH_CODE= :RealRecord.Am_Wth_Code,
PM_WTH_CODE= :RealRecord.Pm_Wth_Code,
AM_RAIN_RATE = TO_NUMBER(:RealRecord.Am_Rain_Rate),
PM_RAIN_RATE = TO_NUMBER(:RealRecord.Pm_Rain_Rate),
MIN_TEMP = TO_NUMBER(:RealRecord.Min_Temp),
MAX_TEMP = TO_NUMBER(:RealRecord.Max_Temp),
MIN_HUMI = TO_NUMBER(:RealRecord.Min_Humi),
MAX_HUMI = TO_NUMBER(:RealRecord.Max_Humi),
WTH_INFO = RTRIM(:RealRecord.Wth_Info)
WHEREWTH_DATE = :RealRecord.Wth_Date
ANDCode = SUBSTR(:RealRecord.Code, 2, 2);
}

if (SQLCODE != SQL_OK) {
DB_Error(&Pg_Info[0]);
printf("\n[%d]\n[%s]\n",SQLCODE,SQLERRTXT);
Log_Line(Pg_Info[0],"####### 날씨 Update Error Data(TTNMMM) #######\n");
DataLogging();
return -1;
}
else {
printf("####### Update OK #######");
}
}

Tot_Cnt = Tot_Cnt + 1;

memset(Input_Data,0x00,sizeof(Input_Data));
memset(Input_Buf ,0x00,sizeof(Input_Buf ));
Read_Chk = fread(Input_Buf ,sizeof(Input_Data) + 1,1,fi);

strncpy(Input_Data, Input_Buf+1, sizeof(Input_Data));

}

printf("---------------------------------------\n");
printf("총 Data 건수 : [%d]\n", Tot_Cnt);

MEM(Log_Text);
sprintf(Log_Text, "\n총 Data 건수 : [%d]\n", Tot_Cnt);

return 0;
}


/*=================================================*/
/* 함수명 : File_Open()*/
/* 기능 : 날씨 정보 SAM File Open*/
/*=================================================*/
int File_Open()
{
char *open_in_file = "./nalssi.dat";

fi = fopen(open_in_file, "rb");
if (!fi) {
printf("\ 날씨 파일이 없습니다.... \n", stderr);
return 1;
}
return 0;
}


/*===========================================================*/
/* 함수명 : Save_DataFile()*/
/* 기능 : 사용한 파일은 별도의 디렉토리에 이름을 바꿔 보관 */
/*===========================================================*/
void Save_DataFile() {
charbuff[250];
charNow [14+1];

memset(Now,0x00,sizeof(Now));
memset(buff, 0x00, sizeof(buff));

EXEC SQL
SELECT TO_CHAR(SYSDATE, 'yyyymmddhh24miss')
INTO :Now
FROM DUAL;

sprintf(buff, "mv ./nalssi.dat ./data/nalssi.dat_%s", Now);

printf("Command in => [%s] \n",buff);

system(buff);
}


/*======================================================*/
/* 함수명 : File_Close()*/
/* 기능 : 날씨 정보 SAM File Closing*/
/*======================================================*/
void File_Close()
{
fclose(fi);
}

/*======================================================*/
/* 함수명 : DataLogging() */
/* 기능 : Insert 오류가 발생한 데이터를 로깅*/
/*======================================================*/
void DataLogging() {

char Log_Text[1024];
MEM(Log_Text);

printf("Read Data Stream => [%s] \n",Input_Data);
printf("Read Data Check1 => [%s][%s][%s][%s][%s][%s][%s][%s][%s][%s]\n",
RealRecord.Wth_Date,RealRecord.Code,
RealRecord.Am_Wth_Code, RealRecord.Pm_Wth_Code,
RealRecord.Am_Rain_Rate, RealRecord.Pm_Rain_Rate,
RealRecord.Max_Temp,RealRecord.Min_Temp,
RealRecord.Max_Humi,RealRecord.Min_Humi
);
printf("Read Data Check2 => [%s] \n\n",RealRecord.Wth_Info);

sprintf(Log_Text, "Insert/Update Error Data :::: [%s][%s][%s][%s][%s][%s][%s][%s][%s][%s]\n",
RealRecord.Wth_Date,RealRecord.Code,
RealRecord.Am_Wth_Code, RealRecord.Pm_Wth_Code,
RealRecord.Am_Rain_Rate, RealRecord.Pm_Rain_Rate,
RealRecord.Max_Temp,RealRecord.Min_Temp,
RealRecord.Max_Humi,RealRecord.Min_Humi
);

Log_Line(Pg_Info[0],Log_Text);
Log_Line(Pg_Info[0],"####################################\n");
}

, .