logo资料库

c语言读取csv.doc

第1页 / 共2页
第2页 / 共2页
资料共2页,全文预览结束
include #include #define MAX_LINE 128 typedef { struct char week[4]; char month[4]; char date[3]; char hour_minute_second[9]; char year[5]; } system_time_t; { value; FILE *fp; float system_time_t system_time; char line[MAX_LINE]; void main() if((fp=fopen("d:\\1.csv","r")) { == NULL) printf("Can't open d:\\1.csv\n"); } memset(&system_time, 0, while { (fgets(line, MAX_LINE, fp)) sizeof(system_time_t)); , system_time.month, system_time.date, sscanf(line, "%f %s%s%s%s%s", &value, system_time.week, system_time.hour_minute_second, system_time.year); printf("value=%f system_time=%s %s %s %s %s\n", value, system_time.week, system_time.month, system_time.date, system_time.hour_minute_second, system_time.year); } } struct record_t { double money; time_t tim; };
typedef struct record_t record; static const char *const short_months[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; //把每行转成 record 的数据结构。 record covert(char* inputval) { record retval; char* comapos; char weekday[4] = {'\0'}, mon[4]={'\0'}; struct tm gtm; int n; assert(inputval != null); comapos = strstr(inputval, assert(comapos != null); *comapos = '\0';; retval.money = atof(inputval); ","); sscanf(comapos+1, "%s %s %d %d:%d:%d %d", weekday, mon, gmt.tm_mday, gmt.tm_hour, gmt.tm_min, gmt.tm_sec gmt.tm_year); for (n=0; n <12; n++) if (strcmp(mon, short_months[n]) == 0) break; gmt.tm_mday = n; gmt.tm_year -= 1900; gmt.tm_isdst = -1; retval.tim = mktime(&gmt) + GMTOFF(gmt); return retval; } 其他的很简单了,从中间读一行,判断和你要的时间的大小,大则读后面的中间一行,小则 读前面的一半的一行,通过递归读出最你需要的那一行。
分享到:
收藏