c++ function making for a file reading through delimiter but in different style -
i want make parser , read line txt file file like:
indore|nj|201568|a| ujjain|ab|3458|b|
is inbuilt function read 1 line @ time... catch values in correspoding variable, through format specifier.. separate values |
delimiter.
parser(str,'|',"%s|%s|%s|%s",location,name,phno,tok);
location
,name
,...etc variables.
i found function in matlab "strread", type of thing.
assuming str
is stream, since variables appear strings, can simple use std::getline
'|'
delimiter.
std::getline(str, location, '|'); std::getline(str, name, '|'); std::getline(str, phno, '|'); std::getline(str, tok, '|');
you wrap variadic template function takes parameters want read into.
void readparams(std::istream& stream, char delim) { } template<typename ... tail> void readparams(std::istream& stream, char delim, std::string& string, tail& ... t) { std::getline(stream, string, delim); readparams(stream, delim, t ...); }
this function can called such:
readparams(str, '|', location, name, phno, tok);
if restricted using compiler without variadic templates, can still add few overloads number of arguments want, , replace them true variadic template @ later stage.
Comments
Post a Comment