Hi All,

i would need some advice from you guys from this forum to solve the reading of the CSV file that i had.

for example, i have a file which consist of about 2000 records (below are the example of the records)

10/10/2013 10:25:09 AM,5.89,45,264.83,PTTTT
10/10/2013 10:25:13 AM,5.89,2000,11780.00,

Some of the records are with the "PTTTT" and some dont.
So how do i read the records for the part until 4th column and elimiate the PTTTT column ?

Thanks

Recommended Answers

All 5 Replies

Can you paste about 10 lines or so from the file into here. Does every column value have a "," after it or does each line end with a newline? Generally with a CSV file if there is an empty column it is represented as ",," but I don't see that in your example. How you read in the file depends greatly on how the file format is defined.

NathanOliver, below is more of the CSV file:

10/10/2013 10:06:21 AM,5.89,1,5.89,CTTT
10/10/2013 10:06:21 AM,5.89,816,4806.24,
10/10/2013 10:06:21 AM,5.89,900,5301.00,
10/10/2013 10:06:21 AM,5.89,602,3545.78,
10/10/2013 10:06:21 AM,5.89,1783,10501.87,
10/10/2013 10:06:21 AM,5.89,94,553.66,TXXU
10/10/2013 10:06:21 AM,5.89,805,4741.45,
10/10/2013 10:05:45 AM,5.88,55,323.40,VBAY
10/10/2013 10:05:34 AM,5.88,504,2963.52,
10/10/2013 10:05:34 AM,5.89,1,5.89,PSSSXX
10/10/2013 10:04:49 AM,5.88,2115,12436.20,

commented: CSV is the Comma Seperated File in which we can any seperator in place of comma +0

It is really weird that the lines with the text at the end don't have a coma where the lines with a number do. Since you do not need the extra column you should be able to get away with using a stringstream to split the columns. I would do something like the following. I am not sure how you are storing the information from the file in your program so in my example each part will just be a string stored in a vector. Each vector will be stored in a vector of vectors.

std::string line;
std::vector<std::vector<std::string>> lines;
stringstream ss;
ifstream fin("filename.csv");

while (getline(fin, line))
{
    std::string temp;
    std::vector<std::string> parts;
    // load line in stringstream
    ss << line;
    // we need for columns only so use a loop 4 times
    for (int i = 0; i < 4; i++)
    {
        getline(ss, temp, ',');
        parts.push_back(temp);
    }
    lines.push_back(parts);
    ss.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    ss.clear();
}

CSV format will end a line with , if the last field is empty.

If you use a standard CSV parser you would just be able to select all but the last column (through the API of the library). Search for C++ CSV parser and you will get plenty of options.

#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;

int main() {

    ifstream input_from_file("test.csv");

    string line;

    while (getline(input_from_file, line)) {

        line += ",";

        stringstream ss(line);

        string word;

        vector<string> words;

        while (getline(ss, word, ','))
            words.push_back(word);

        cout << words.size() << endl;
    }

    return 0;
}

The line line += ","; should suffice.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.