I have a dat file like this
AAAA     1861    9   7   0.00    -1.50   -19.50      9999.00     9999.00     9999.00     9999.00     18.20   9999.00     6.70    135.00      8.00    9999.00     9999.00     9999.00     9999    0   193        250180280600000000 
BBBBBBBBB    1861    9   7   0.00    -7.50   36.50   9999.00     9999.00     21.40   1018.10     22.60   9999.00     1.00    68.00   2.00    9999.00     9999.00     9999.00     9999    0   193        451720280600000000 
VVVVVVVVV    1861    9   7   0.00    -58.50      10.50   9999.00     9999.00     27.50   9999.00     26.90   9999.00     15.40   45.00   6.00    9999.00     9999.00     9999.00     9999    0   193        357610280600000000 
SSSSSSSS     1861    9   7   0.00    117.50      13.50   9999.00     9999.00     28.00   1010.30     28.00   9999.00     4.60    158.00      5.00    9999.00     9999.00     9999.00     9999    0   193        370170280600000000 
DD   1861    9   7   1.00    -19.50      6.50    9999.00     9999.00     24.70   1014.80     25.30   9999.00     6.70    203.00      2.00    9999.00     9999.00     9999.00     9999    0   193        343600280600000000 
E    1861    9   7   1.00    44.50   -28.50      9999.00     9999.00     20.40   9999.00     19.00   9999.00     2.60    135.00      2.00    9999.00     9999.00     9999.00     9999    0   193        218240280600000000 
ZZZZZZZZZZZZ     1861    9   7   1.00    -15.50      38.50   9999.00     9999.00     22.00   9999.00     21.90   9999.00     6.70    68.00   2.00    9999.00     9999.00     9999.00     9999    0   193        458840280600000000 
and I would like to store the first column of the file in a vector of strings. Any idea?
This is what I tried
for(int k=0;k<ROW;k++){
   INNA >> ID;                      
   for(int j=1;j<=COL;j++) INNA >> discardID; 
   IDname[k]= ID;
} 
but in ID I then have all the columns as a unique string..
Many thanks
This is how I would solve it:
#include <fstream>
#include <strtk.hpp>   // http://www.partow.net/programming/strtk
std::string filename("your_input_data.txt");
// assuming the file is text
std::fstream fs;
fs.open(filename.c_str(), std::ios::in);
if(fs.fail())  return false;   
const char *whitespace    = " \t\r\n\f";
std::string keyword;
std::vector<float> floats;
std::vector<string> keywords;
// process each line in turn
while( std::getline(fs, line ) )
{
// remove beginnning and ending whitespace
// can prevent parsing problems
// from different line endings.
strtk::remove_leading_trailing(whitespace, line);
    // strtk::parse combines multiple delimeters in these cases
    if( strtk::parse(line, whitespace, keyword, floats ) ) 
    {
         std::cout << "succeed" << std::endl;
     // keyword contains the string and the rest of the values
     // are placed in the floats vector
     keywords.push_back( keyword );
     // you can also process the numbers
    }
}
                        Read the whole line then get the first column.
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
std::vector<std::string> vec;
std::string line;
while (std::getline(std::cin, line)) {
    std::istringstream strm(line);
    std::string firstcol;
    strm >> firstcol;
    vec.push_back(firstcol);
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With