Pro C 파일 읽어서 DB에 넣는 부분 관련 질문입니다..

army7912의 이미지

file:

DATE=2006/12/28;
GENERATOR=2252;
METER=22520211;
FORMAT=PF;CH1;CH2;CH3;CH4;


00:00;0;27104;0;3255;0;
00:05;0;26912;0;3328;0;
00:10;0;27022;0;3232;0;

읽어오는 파일의 형식은 위와 같습니다.

source:
int insert_ETFP01T1()
{
FILE *myfile;
int close_yn=0;
char filename[23]="\0";
char buffer[30]="\0";
int ratio_idx=0;
char max_fep_time[6]="\0";
int isdata = 0;
int inserting = 0;
int i=0;
int datacount=0;

int file_exist=0;

int mdata[5]={-1,-1,-1,-1,-1};
double kwdata[5];
char * p="\0";
char fep_time[6]="\0";
char fep_time_old[6]="\0";
int count=0;
char genHH[3]="\0";
char genMM[3]="\0";
char temp[10]="\0";

double dConstM1=0, dConstM2=0, dConstM3=0;

memset(p, 0x00, 10);

sprintf(filename,"pl-%s-%s.txt", &cur_time[1], code);

printf("파일명 %s\n",filename);

myfile=fopen(filename,"r");

if(myfile==NULL){
printf("%s 파일이 없음\n",filename);
return file_exist;
}
file_exist=1;

while(!feof(myfile)){
isdata=0;
if(fgets(buffer,80,myfile)==NULL) break;

printf("************************************%d, %s", strncmp(buffer,"",6), buffer);

if(strncmp(buffer,"",6) ==0){
isdata=1;
continue;
}else if(strncmp(buffer,"",7)==0) {
EXEC SQL COMMIT;
close_yn=fclose(myfile);
return file_exist;
}

/*데이터를 잘라서 각각의 변수에 담는다.*/
if(isdata==1)
{
if(strstr(buffer,";;;;;;") != NULL)
{
continue;
}

/*p=strtok(buffer,";");*/
strcpy(p,strtok(buffer,";"));

if(p)
{
strcpy(fep_time,p);
}

strcpy(fep_time_old,fep_time);

while(p!=NULL)
{
/*p=strtok(NULL,";");*/
strcpy(p,strtok(NULL,";"));

if(p)
{
sprintf(temp,p);
if(temp != NULL)
{
mdata[count]= atoi(p);
}
}
count++;
}

strcpy(fep_time_old,fep_time);
/*p=strtok(fep_time,":");*/
strcpy(p,strtok(fep_time,";"));

if(p) sprintf(genHH,p);
/*p=strtok(NULL,":");*/
strcpy(p,strtok(NULL,";"));
if(p) sprintf(genMM,p);

while(1){
EXEC SQL SELECT count(*) INTO datacount FROM ETFP01T1
WHERE DGEN_YMD= TO_DATE(:&cur_time[1],'YYYYMMDD') AND GEN_CODE=:code and GEN_HH=:genHH AND GEN_MM=:genMM;

if ( sqlca.sqlcode != 0 ) printf("Get MAX_FEP_TIME %s\n", sqlca.sqlerrm.sqlerrmc);

break;
}
}

파일 읽어서 buffer에 담아서 비교하는 부분입니다.

printf("************************************%d, %s", strncmp(buffer,"",6), buffer);

이렇게 buffer 내용을 찍어봤는데 strncmp(buffer,"",6) 이 값이 0이 나와야 하는데.. 막상 화면에 찍히는걸 보면

60으로 찍혀나옵니다.

printf에 \n 표시가 없어서 내용이 이어져서 좌르륵 나와야 하는거 아닌가요?

원본 파일 자체가..

00:00;0;27104;0;3255;0;
00:05;0;26912;0;3328;0;

이런식으로 엔터가 먹혀있어서 그런가 buffer 내용을 찍어보면 엔터가 먹혀져서 한줄씩 나옵니다.

그거때문에 strncmp(buffer,"",6) 여기서 0값이 안나오는걸까요?

0값이 나와야 isdata=1 이게 먹혀서..

if(isdata==1)
{
if(strstr(buffer,";;;;;;") != NULL)
{
continue;
}

이쪽으로 들어와서 처리가 되야 하는데 안되고 있네요..

원인이 뭘까요;

익명사용자의 이미지

사실상 코드로 봐서는 의미가 전달되지 않습니다. (실행이 되지 않는 코드이므로 당연한 것이기도 하지만... 함수 자체도 의도와 다르게 쓰인 것으로 보이는 부분이 많습니다.)

일단 대강 보고 나서 생긴 의문점:

1. fgets 함수는 무조건 지정한 길이만큼 읽어들입니다.
(\n에서 끊어주는 함수가 아닙니다.)
위의 데이터에서 한줄 넘게 읽는 겁니다.
그리고 그 데이터를 읽는 버퍼의 크기가 훨씬 작습니다.
2. 변수 p는 init data 영역을 오염시키고 있습니다. p를 배열로 할당하십시오.
3. strncmp 함수는 두 문자열을 비교하여 같을 때 0을 리턴합니다.
데이터와 사용된 함수를 볼 때 0을 기대할 수 없습니다.
비교 문자열의 길이를 6, 7 등으로 지정한 이유가 특별히 있습니까?

ㅡ,.ㅡ;;의 이미지

fgets \n 에서 끊어주는 함수가 맞습니다.
한줄씩 읽습니다.

그리고 질문에서 옆으로 붙어나오진 않겠죠.. 개행문자를 읽었으니까.
당연히 비교도 같다가 아닐테죠.


----------------------------------------------------------------------------

익명사용자의 이미지

지적 감사드립니다. fread와 혼란을 일으킨 것 같습니다. orz

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.