2013-03-21 3 views
-1

이것은 내가 지금까지 가지고있는 것입니다. 나는 파일 day.txt을 읽고 난 텍스트 파일에서 전체 라인을 얻을 수 fgets()를 사용하여 문자열로 넣고 추출 sscanf()을 사용라인 계산 및 C에서 구조체 배열 표시

#include "stdio.h" 
#include "stdlib.h" 

#define MAX_LINE_LENGTH 

struct earthquake_t{ 
    float lat; 
float lng; 
float dep; 
float mag; 
}; 
FILE* fid; 
int main(void) 
{ 
double lat,lng dep,mag; 
double userminlat,userminlng,usermaxlat,usermaxlng,minlat,minlng,maxlat,maxlng,i, ch,line; 
struct earthquake_t *earthquake; 

fid=fopen("day.txt","r"); 

while (!feof(fid)) 
{  /*Need help counting lines*/ 
    count = fscanf(fid," %lf %lf %lf\n",&lng,&lat,&dep,&mag); 
/* count tell you how many of the % fields were matched, 
and you need to make sure all three were found to avoid blank lines*/ 

    if (count ==3) 
    { 
     line ++; 
    } 
} 

fseek(fid,1,SEEK_SET); 
print("%d\n",sizeof(struct earthquake_t));   
earthquake = (struct earthquake_t *)malloc(sizeof(struct earthquake_t)*line); 

for (i=0;i<line;i++) 
{   
    fscanf("fid,%lf %lf %lf\n",&lng,&lati,&elevi) 
    earthquake[i].lat=lat; 
    earthquake[i].lng=lng; 
    earthquake[i].dep=dep; 
    earthquake[i].mag=mag; 


    /*Setting the min and max values for latitude and longatidue*/ 
    if(earthquake[i].lng >maxlng) 
    {  
     maxlng = earthquake[i].lng; 
    } 
    if(earthquake[i].lng >minlng) 
    {  
     minlng = earthquake[i].lng; 
    } 
    if(earthquake[i].lat >maxlat) 
    { 
     maxlat = earthquake[i].lat; 
    } 
    if(earthquake[i].lat >minlat) 
    { 
     minlat = earthquake[i].lat; 
    } 

} 
+3

좋은 직장 ... 당신이 직면하고있는 문제가 무엇인지 말해 줄 수 있습니까? – Pradheep

답변

0

파일에서 라인의 수를 계산하셔야합니다 값. 부분

/* count tell you how many of the % fields were matched, 
and you need to make sure all three were found to avoid blank lines*/ 

scanf man page 상태 :

반환 값이 함수가 성공적으로 일치하고 할당 된 입력 항목의 수를 반환

, 제공, 또는 제로보다 적게 할 수있다 조기 매칭 실패의 이벤트.

첫 번째 성공적인 변환 또는 일치하는 오류가 발생하기 전에 입력 끝나면 값 EOF가 반환됩니다. 읽기 오류가 발생하면 EOF도 반환됩니다.이 경우 스트림에 대한 오류 표시기 (ferror (3) 참조)가 설정되고 errno가 설정되어 오류를 나타냅니다.

빈 줄을 피하려면 line[0] == '\n'을 확인할 수 있습니다.

희망이 있습니다.