C 有没有办法在同时包含数据和时间的列的数据文件中分隔列
所以我要导入一个包含5列的数据文件,例如 1992-01-25T00:00:30.000Z | 0.718 |-0.758 |-0.429 | 1.129 我知道C 有没有办法在同时包含数据和时间的列的数据文件中分隔列,c,database,import,C,Database,Import,所以我要导入一个包含5列的数据文件,例如 1992-01-25T00:00:30.000Z | 0.718 |-0.758 |-0.429 | 1.129 我知道scanf()允许您指定它正在扫描的数据类型,就像在本例中是%s和%f。但我的问题是,对于第一列,我想把它作为一个数字,或者像1992-01-25 | 00:00:30.000那样把它分成两列。使用fgets()是另一种选择吗 有没有一种方法可以有效地做到这一点,因为我将每个列存储到数组中,然后我对每个数组都有一个搜索函数,搜索包含字符
scanf()
允许您指定它正在扫描的数据类型,就像在本例中是%s
和%f
。但我的问题是,对于第一列,我想把它作为一个数字,或者像1992-01-25 | 00:00:30.000那样把它分成两列。使用fgets()
是另一种选择吗
有没有一种方法可以有效地做到这一点,因为我将每个列存储到数组中,然后我对每个数组都有一个搜索函数,搜索包含字符串的数组会很痛苦 如果我是你,我会在解析文件后创建一个保存数据表的结构
typedef struct {
int num_rows;
char table[MAX_NUM_ROWS][MAX_NUM_COLS][MAX_COL_LEN];
} YOUR_DATA;
您应该使用fgets
逐行解析该文件。首先在“T”上标记行,然后在“|”上标记行,类似这样
FILE your_fp;
YOUR_DATA yourTable;
char line[MAX_ROW_LEN] = {0};
char *ptr = NULL, field = NULL;
int row = 0, col = 0;
if ((your_fp = fopen("datafile.txt", "r")) == NULL) {
//error
}
while(fgets(line, sizeof(line), your_fp) != NULL) {
ptr = line;
col = 0;
if ((field = strsep(&ptr, "T")) != NULL) {
snprintf(yourTable.table[row][col], MAX_COL_LEN, "%s", field);
col++;
}
while ((field = strsep(&ptr, "|")) != NULL) {
snprintf(yourTable.table[row][col], MAX_COL_LEN, "%s", field);
col++;
}
row++
}
可能希望跟踪表中的行数等,而不必担心将它们转换为正确的数据类型。如果我是你,我会在解析文件后创建一个保存数据表的结构
typedef struct {
int num_rows;
char table[MAX_NUM_ROWS][MAX_NUM_COLS][MAX_COL_LEN];
} YOUR_DATA;
您应该使用fgets
逐行解析该文件。首先在“T”上标记行,然后在“|”上标记行,类似这样
FILE your_fp;
YOUR_DATA yourTable;
char line[MAX_ROW_LEN] = {0};
char *ptr = NULL, field = NULL;
int row = 0, col = 0;
if ((your_fp = fopen("datafile.txt", "r")) == NULL) {
//error
}
while(fgets(line, sizeof(line), your_fp) != NULL) {
ptr = line;
col = 0;
if ((field = strsep(&ptr, "T")) != NULL) {
snprintf(yourTable.table[row][col], MAX_COL_LEN, "%s", field);
col++;
}
while ((field = strsep(&ptr, "|")) != NULL) {
snprintf(yourTable.table[row][col], MAX_COL_LEN, "%s", field);
col++;
}
row++
}
可能希望跟踪表中的行数等,而不必担心如何将它们转换为正确的数据类型。您可以使用
fgets
、strtok
和sscanf
来解析文件
typedef struct {
int num_rows;
char table[MAX_NUM_ROWS][MAX_NUM_COLS][MAX_COL_LEN];
} YOUR_DATA;
从文件中读取一行fgets
使用strtok
作为分隔符将行拆分为子字符串|
解析子字符串以将每个子字符串转换为数字sscanf
“1992-01-25”成为十进制数
19920125
。时间字段组合在一起,以便最终结果表示从午夜开始的毫秒数
bool parseFile(FILE *fpin)
{
char line[256];
while (fgets(line, sizeof(line), fpin) != NULL)
{
// get the date/time portion of the line
char *dateToken = strtok(line, "|");
// extract the floating point values from the line
float value[4];
for (int i = 0; i < 4; i++)
{
char *token = strtok(NULL, "|");
if (token == NULL)
return false;
if (sscanf(token, "%f", &value[i]) != 1)
return false;
}
// extract the components of the date and time
int year, month, day, hour, minute, second, millisec;
char t, z;
sscanf(dateToken, "%d-%d-%d%c%d:%d:%d.%d%c",
&year, &month, &day, &t,
&hour, &minute, &second, &millisec, &z);
// combine the components into a single number for the date and time
int date = year * 10000 + month * 100 + day;
int time = hour * 3600000 + minute * 60000 + second * 1000 + millisec;
// display the parsed information
printf("%d %d", date, time);
for (int i = 0; i < 4; i++)
printf(" %6.3f", value[i]);
printf("\n");
}
return true; // the file was successfully parsed
}
bool解析文件(文件*fpin)
{
字符行[256];
while(fgets(line,sizeof(line),fpin)!=NULL)
{
//获取行的日期/时间部分
char*dateToken=strtok(行“|”);
//从行中提取浮点值
浮动值[4];
对于(int i=0;i<4;i++)
{
char*token=strtok(NULL,“|”);
if(标记==NULL)
返回false;
如果(sscanf(令牌、%f、&value[i])!=1)
返回false;
}
//提取日期和时间的组成部分
整数年、月、日、时、分、秒、毫秒;
字符t,z;
sscanf(日期令牌,“%d-%d-%d%c%d:%d:%d.%d%c”,
&年、月、日、时,
&小时、分钟、秒、毫秒和z);
//将组件组合为日期和时间的单个数字
整数日期=年*10000+月*100+天;
整数时间=小时*3600000+分钟*60000+秒*1000+毫秒;
//显示解析后的信息
printf(“%d%d”,日期、时间);
对于(int i=0;i<4;i++)
printf(“%6.3f”,值[i]);
printf(“\n”);
}
return true;//文件已成功解析
}
您可以使用fgets
、strtok
和sscanf
解析文件
typedef struct {
int num_rows;
char table[MAX_NUM_ROWS][MAX_NUM_COLS][MAX_COL_LEN];
} YOUR_DATA;
从文件中读取一行fgets
使用strtok
作为分隔符将行拆分为子字符串|
解析子字符串以将每个子字符串转换为数字sscanf
“1992-01-25”成为十进制数
19920125
。时间字段组合在一起,以便最终结果表示从午夜开始的毫秒数
bool parseFile(FILE *fpin)
{
char line[256];
while (fgets(line, sizeof(line), fpin) != NULL)
{
// get the date/time portion of the line
char *dateToken = strtok(line, "|");
// extract the floating point values from the line
float value[4];
for (int i = 0; i < 4; i++)
{
char *token = strtok(NULL, "|");
if (token == NULL)
return false;
if (sscanf(token, "%f", &value[i]) != 1)
return false;
}
// extract the components of the date and time
int year, month, day, hour, minute, second, millisec;
char t, z;
sscanf(dateToken, "%d-%d-%d%c%d:%d:%d.%d%c",
&year, &month, &day, &t,
&hour, &minute, &second, &millisec, &z);
// combine the components into a single number for the date and time
int date = year * 10000 + month * 100 + day;
int time = hour * 3600000 + minute * 60000 + second * 1000 + millisec;
// display the parsed information
printf("%d %d", date, time);
for (int i = 0; i < 4; i++)
printf(" %6.3f", value[i]);
printf("\n");
}
return true; // the file was successfully parsed
}
bool解析文件(文件*fpin)
{
字符行[256];
while(fgets(line,sizeof(line),fpin)!=NULL)
{
//获取行的日期/时间部分
char*dateToken=strtok(行“|”);
//从行中提取浮点值
浮动值[4];
对于(int i=0;i<4;i++)
{
char*token=strtok(NULL,“|”);
if(标记==NULL)
返回false;
如果(sscanf(令牌、%f、&value[i])!=1)
返回false;
}
//提取日期和时间的组成部分
整数年、月、日、时、分、秒、毫秒;
字符t,z;
sscanf(日期令牌,“%d-%d-%d%c%d:%d:%d.%d%c”,
&年、月、日、时,
&小时、分钟、秒、毫秒和z);
//将组件组合为日期和时间的单个数字
整数日期=年*10000+月*100+天;
整数时间=小时*3600000+分钟*60000+秒*1000+毫秒;
//显示解析后的信息
printf(“%d%d”,日期、时间);
对于(int i=0;i<4;i++)
printf(“%6.3f”,值[i]);
printf(“\n”);
}
return true;//文件已成功解析
}
是否可以按空格而不是|标记,因为文件是按空格分隔的。这是否只需要一个“.”YGarcia是的,如果列之间用空格分隔,那么“.”就可以了。@user3386109我有一个问题,关于如何将这些数据插入到链接列表中。我创建了一个名为insert的函数(SortedLinkedList*list、int-date、int-Time、float-rmag、float-NSmag、float-azmag、float-avgmag)。我希望将每一行数据放在一个单独的节点中。但是声明的值数组是否包含每行或每列