-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.c
More file actions
44 lines (40 loc) · 1.14 KB
/
parse.c
File metadata and controls
44 lines (40 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "parse.h"
int parse_csv(char* dataset_name, data_t features[ROWS][FEATURE_LENGTH], char metadata[ROWS][2][20]) {
char *line;
char *record;
char buffer[1000000];
int row_nb=0, col_nb=0;
int i,j;
FILE *dataset;
printf(" Parsing %s ... \n ", dataset_name);
dataset = fopen(dataset_name,"r");
if(dataset==NULL){
printf("Error opening file %s\n",dataset_name);
return -1;
}
//skip first line with columns title
line=fgets(buffer,sizeof(buffer),dataset);
while((line=fgets(buffer,sizeof(buffer),dataset))!=NULL){
record=strtok(line,",");
//store author data
strcpy(metadata[row_nb][0],record);
//store title data
record=strtok(NULL,",");
strcpy(metadata[row_nb][1],record);
record=strtok(NULL,",");
col_nb=0;
while(record!=NULL)
{
data_t tmp = strtod(record,NULL);
features[row_nb][col_nb]=tmp;
col_nb++;
record=strtok(NULL,",");
}
row_nb++;
}
printf("%d %d \n", row_nb+1, col_nb);
fclose(dataset);
}