#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
// 2014年4月1日 08:30からの経過時間(分)から日時を出す
int minutes2dateAndTime(int min, char *s)
{
// 1900年から、2014年4月1日 08:30までの経過秒数→1396308600秒
time_t fixed_sec = min * 60 + 1396308600; //1900年からの秒数に変換
struct tm *tm = localtime(&fixed_sec);
// 文字列にして戻す(別の形式でも良いが、とりあえず"2018/1/1 0:00"の形で)
// mainの方でメモリ確保して貰って、こっちで書き込むという形にする(コピーが面倒だから)
memset(s,0,sizeof(s));
sprintf(s,"%d/%02d/%02d %02d:%02d",tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min);
return 0;
}
// 2014年4月1日 08:30からの経過時間(分)を算出する
int dateAndTime2minutes(char *s1)
{
char s2[] ="/ :"; // デリミタは"/", " ", ":" の3つ
// char s1[] ="2014/4/3 8:08";
char* tok;
// 2014年4月1日8:30の累積分数
struct tm base_tm;
base_tm.tm_year = 2014 - 1900; // 2014年
base_tm.tm_mon = 4 - 1; // 4月
base_tm.tm_mday = 1; // 1日
base_tm.tm_hour = 8; // 8時
base_tm.tm_min = 30; // 30分
base_tm.tm_sec = 0; // 0秒
time_t time = mktime(&base_tm);
int base_min = (int)time/60;
///////
struct tm tm;
tok = strtok( s1, s2 ); /* 初回 */
tm.tm_year = atoi(tok)-1900;
tok = strtok( NULL, s2 ); /* 2回目*/
tm.tm_mon = atoi(tok) -1 ;
tok = strtok( NULL, s2 ); /* 3回目 */
tm.tm_mday = atoi(tok);
tok = strtok( NULL, s2 ); /* 4回目 */
tm.tm_hour = atoi(tok);
tok = strtok( NULL, s2 ); /* 5回目 */
tm.tm_min = atoi(tok);
tm.tm_sec = 0; //秒はデフォルト0
time = mktime(&tm);
int target_min = (int)time/60;
return target_min - base_min;
}
int main()
{
char *f_loader = "C:/Users/ebata/Desktop/LDSD.csv";
FILE* fp_loader = fopen( f_loader, "r" );
if( f_loader == NULL ){
printf( "%sファイルが開けません\n", f_loader );
return -1;
}
char s1[200]; //200文字以上ということはないだろう
char s2[] =","; /* デリミタはコンマのみ(なぜこれで動く?) */
char* tok;
char facility_string[100][10];
memset(facility_string,0,sizeof(facility_string));
char arrival_time_string[100][20];
memset(arrival_time_string,0,sizeof(arrival_time_string));
char start_time_string[100][20];
memset(start_time_string,0,sizeof(start_time_string));
char train_name_string[100][20];
memset(train_name_string,0,sizeof(train_name_string));
// 一行目(見出し行)の強制スキップ
fgets( s1,200,fp_loader);
int line =0;
// 一度、全データを読み込む
while( ( fgets( s1,200,fp_loader) != NULL)){
//printf("-->%s",s1);
tok = strtok( s1, s2 );
int count = 0;
while( tok != NULL ){
printf("count = %d\n",count);
if (count == 0) strcpy(facility_string[line],tok);
else if(count == 1) strcpy(arrival_time_string[line],tok);
else if (count == 2) strcpy(start_time_string[line],tok);
else if (count == 3) strcpy(train_name_string[line],tok);
printf( "%s\n", tok );
tok = strtok( NULL, s2 ); /* 2回目以降 */
count++;
}
line++;
}
int base_line = line;
fclose( fp_loader );
int arrival_time_min[100]; // 到着時刻
memset(arrival_time_min,0,sizeof(arrival_time_min));
int start_time_min[100]; // 出発時刻
memset(start_time_min,0,sizeof(start_time_min));
char train_name_buf_string[100][20]; // 名一時格納用
memset(train_name_buf_string,0,sizeof(train_name_buf_string));
//////////////////// 2110 ////////////////////////////////////
int k = 0;
for(int i=0; i%s",s1);
tok = strtok( s1, s2 );
int count = 0;
while( tok != NULL ){
//printf("count = %d\n",count);
if (count == 0) strcpy(batch_number_string[line2],tok);
else if(count == 1) strcpy(batch_start_time_string[line2],tok);
else if (count == 2) strcpy(batch_end_time_string[line2],tok);
//printf( "%s\n", tok );
tok = strtok( NULL, s2 ); /* 2回目以降 */
count++;
}
line2++;
}
fclose(fp_train);
return 0;
}