getline() function bunches the headings together
-
22-04-2021 - |
Question
The first row of my data file is the headers for the three (or four) columns of data:
%b02_a08 b02_a08_counts b02_a08_eu
As you can see, the columns are TAB delimited. When I use getline()
, it mashes the whole line together. If I use getline(,,'\t')
, it returns %b02_a08
for the line. I need the line separated out, because I read each header.
If anyone knows of a way to read the headers from a data file and put them into a vector<string>
so that I can run Regex on them to find the one with "eu" in it, that is all I need to do. Well, that and read the columns of data, which I will probably run into the same TAB delimited problem, there.
Solution
Use the stream Luke...
as in,
// process the header
std::getline(some_input_stream, line))
{
std::istringstream str(line);
// Now reaad each field
str >> header_1 >> header_2 >> header_3;
if (str >> header_4)
{
// process the optional column
have_col_4 = true;
}
}
// Now to read the rows
while(some_input_stream >> col_1 >> col_2 >> col_3)
{
if (have_col_4)
some_input_stream >> col_4;
}
The last block can be done with std::getline
too (i.e. read a line, construct a stream, then read from the stream), however this tends to be somewhat slower...