I'm trying to read a bunch of data from mmap and this functions as I expect it to.
size_t filesize = getFilesize(argv[1]);
int fd = open(argv[1], O_RDONLY, 0);
assert(fd != -1);
char* mmappedData = static_cast<char*>(mmap(NULL, filesize, PROT_READ, MAP_PRIVATE | MAP_POPULATE, fd, 0));
assert(mmappedData != MAP_FAILED);
char *strmmap = strdup(mmappedData);
char *strData = strtok(strmmap, "#");
strData = strtok(strData, ";");
string rid;
unsigned long int timestamp;
string tid;
string TIMESTAMP;
Here is some of the test data from mmap
1497648366867,{75: 5, 76: 2, 77: 4, 78: 1, 79: 0, 80: 3}
79;ns]D;1497648366929
77;_1[A;1497648366940
78;~E=);1497648366940
78;~E=);1497648366943
77;_1[A;1497648366947
80;QXD=;1497648366991
78;E}Hy;1497648366991
78;E}Hy;1497648366997
80;QXD=;1497648367004
I'm struggling with the string manipulation
do{
timestamp = strtoul(strData, NULL, 0);
strData = strtok(NULL, ";");
tid = strData;
cout <<"tid:"<<tid << '\t' << "timestamp:"<<timestamp <<endl;
} while((strData = strtok(NULL, ";")) != NULL);
cout <<"================================="<<endl;
the timestamp variable returns the timestamp perfectly for the first line of the test_data, but fails for the rest of it.
What I'm trying to achieve is something like this, assuming lines is an array of the data split with semicolon if there exist three data points, or split with comma if there are two data points. Here is a prototype in python.
for l in range(len(lines)):
if len(line[l]) == 2:
timestamp = line[l][0]
tids = line[l][1]
else:
tid = line[l][0]
rid = line[l][1]
timestamp = line[l][3]