C++ reading CSVs
Im having a bit of trouble reading CSVs. I have multiple types of data, so i am not sur开发者_如何转开发e how to get this to work:
string, string, bool, bool, int
I cant simply use >>
to read in the data since the deliminator is not whitespace. scanf
doesnt work, since it needs a human input, not file input, getline
only reads in strings and also includes the \n
char for some reason.
how can i read my csv properly?
You CAN use getline. There's an overload where the third argument passed can be a char for the delimiter. Just throw it all in a loop
Another option (which isn't typically recommended for C++, though), is fscanf
. You're right that scanf
is no good for you, but fscanf
is its file-based equivalent.
Another canonical solution typically employed in C, but which isn't so strongly recommended in C++, is to go ahead and use getline
, and then use strtok
or a simple parser to parse each line.
精彩评论