Acquiring Last Column of CSV File

How do I only get the last column of my CSV file and add it to a 1D vector string, for instance the yes/no in to the 1D vector and ignoring the other double values.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  stringstream lineStream(line);
    string bit;

    while (getline(inputFile, line))
    {


        stringstream lineStream(line);
        bit = "";

        getline(lineStream, bit, ',');
        getline(lineStream, bit, '\n');
        getline(inputFile, line);
        stringVector.push_back(bit);
    }


My CSV File
1
2
3
4
5
6
5.1,3.5,1.4,0.2,no
4.9,3.0,1.4,0.2,yes
4.7,3.2,1.3,0.2,no
4.6,3.1,1.5,0.2,yes
5.0,3.6,1.4,0.2,no
5.4,3.9,1.7,0.4,yes
The string class has a function called find_last_of.
Just take everything after it.
Use std::basic_string::rfind https://en.cppreference.com/w/cpp/string/basic_string/rfind

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
#include <vector>
#include <iomanip>

std::string trim( std::string str ) // remove leading and trailing ws
{
    // knock off trailing spaces
    while( !str.empty() && std::isspace( str.back() ) ) str.pop_back() ;

    // get the position of the first non-ws character
    std::size_t pos = 0 ;
    while( pos < str.size() && std::isspace( str[pos] ) ) ++pos ;

    return str.substr(pos) ; // return substring from the first non-ws character
}

std::string last_fld_of( const std::string& csv )
{
     const auto pos = csv.rfind( ',' ) ; // position of the last comma

     if( pos != std::string::npos ) // found last comma
        return trim( csv.substr(pos+1) ) ; // return the fld after the comma

     else return trim(csv) ; // did not find a comma, return the entire string
}

std::vector<std::string> get_last_flds( std::istream& stm )
{
    std::vector<std::string> result ;

    std::string line ;
    while( std::getline( stm, line ) ) // for each line in the file
        result.push_back( last_fld_of(line) ) ;// move the last fld to vector

    return result ;
}

int main()
{
    const std::string file_name = "my_csv.csv" ;

    {
        // create a test file
        std::ofstream(file_name) << "5.1,3.5,1.4,0.2,no\n" // 1
                                    "4.9,3.0,1.4,0.2, yes \n" // 2
                                    "4.7,3.2,1.3,,no\n" // 3
                                    "\n" // 4
                                    "YES\n" // 5
                                    "4.6,3.1,1.5,0.2,yes \n" // 6
                                    "5.0,3.6,1.4,0.2,  no\n" // 7
                                    "5.4,3.9,1.7,0.4,yes\n" ; // 8
    }

    std::ifstream file(file_name) ; // open the file for input
    const std::vector<std::string> last_flds = get_last_flds(file) ;
    for( std::size_t i = 0 ; i < last_flds.size() ; ++i )
        std::cout << "last fld of line #" << i+1 << ' ' << std::quoted( last_flds[i] ) << '\n' ;
}

http://coliru.stacked-crooked.com/a/89de6091646731aa
Hello jlouie6,

Working with what you started with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
std::string line, bit1, bit2, bit3, bit4, lastCol;
std::vector<std::string> stringVector;

while (getline(inputFile, line))
{
	std::istringstream lineStream(line);
	//bit = "";

	std::getline(lineStream, bit1, ',');
	std::getline(lineStream, bit2, ',');
	std::getline(lineStream, bit3, ',');
	std::getline(lineStream, bit4, ',');
	std::getline(lineStream, lastCol);  // <--- Only two parameters needed.

	stringVector.push_back(lastCol);
}

The added advantage is that you could define a struct to hold each variable and push the struct into the vector for later use.

Hope that helps,

Andy
Topic archived. No new replies allowed.