c++ - comma separated stream into struct -
i have structure int , 2 strings. when reading in file comma seperated first 2 values , last value terminated newline. third argument empty however.
ex data: 7, john doe, 123-456-7891 123 fake st.
i want make program grab first number , put in int, find comma , put second number in struct's string etc.
first question should use class instead? have seen getline(stream, mystring, ',');
arguments different data types can't throw them vector.
my code:
struct person{ int id;//dont care if unique string name; string extrainfo; }; int main(int argc, char* argv[]){ assert( argc ==2 && "invalid number of command line arguments"); ifstream inputfile (argv[1]); assert( inputfile.is_open() && "unable open file"); }
what best way of storing information , retrieving file comma separated first 2 , ends newline? want program ignore blank lines in file.
i'd read file line-by-line using normal getline()
. then, put stringstream
further parsing or use string
's find()
functions split text manually.
some more notes:
- i don't understand first question using class. if mean
person
, answer doesn't matter. - using assert don't have control on wrong, argc. should used verify didn't make programming error. also, if #define ndebug, asserts gone, shouldn't part of program logic. throw std::runtime_error("failed open file") instead.
- you don't want double quotes in strings. also, might want
"a,b"
not split comma. make sure have tests assert required functionality.
Comments
Post a Comment