CSV file data extraction

Hey all,

I've got a CSV file with data in a changing number of rows, but fixed columns, with the same data types coming in, but that will be a different pull every time.

I need to take the data from the file and manipulate it. Specifically, I was hoping to use one of the strings to use the find() function to search out specific phrases and count each time that occurred.

I had it originally like this and got nothing:
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
#include "stdafx.h"
#include "EventClass.h"
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

int main()
{
	ifstream SQLPull;
	int  numberOfEvent, totalEvents = 0;
	string number, title1, entity1, area1, tooltype, date1;

	vector<string> QEFNumbers;
	vector<string> QEFtitles;
	vector<string> QEFentity;
	vector<string> QEFtoolset;
	vector<string> QEFdate;

	numberOfEvent = 0;

	

	if (SQLPull)
	{

		while (!SQLPull.eof())
		{
			getline(SQLPull, number, ',');
			cout << number << endl; //to see if it was working. Got nothing.
			getline(SQLPull, title1, ',');
			getline(SQLPull, entity1, ',');
			getline(SQLPull, area1, ',');
			getline(SQLPull, tooltype, ',');
			getline(SQLPull, date1, ',');
			//QEFNumbers[numberOfEvent] = number;
			//QEFtitles[numberOfEvent] = title1;
			//QEFtoolset[numberOfEvent] = tooltype;
			//QEFdate[numberOfEvent] = date1;
			numberOfEvent++;
		}
		
		}
	else
	{
		cout << "Error opening file." << endl;

	}

	SQLPull.close();

    return 0;
}


Then moving the first section out of the nested if and while statement, ex:

1
2
getline(SQLPull, number, ',');
cout << number << endl; 


This returned a jumbled mess of random lines from the CSV file.

I'm trying to figure out how to use sstream, but i'm lost with how to implement. Any help would be appreciated.
Last edited on
You aren't opening the file anywhere.

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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

struct QEF {
    string number;
    string title;
    string entity;
    string area;
    string toolset;
    string date;
};

ostream& operator<<(ostream& os, const QEF& q) {
    return os << q.number  << ':' << q.title << ':' << 
                 q.entity  << ':' << q.area  << ':' << 
                 q.toolset << ':' << q.date;
}

int main() {
    ifstream SQLPull("filename");
    if (!SQLPull) {
        cout << "Error opening file.\n";
        return 1;
    }

    vector<QEF> qef;
    QEF in;
    while (getline(SQLPull, in.number, ',')) {
        getline(SQLPull, in.title, ',');
        getline(SQLPull, in.entity, ',');
        getline(SQLPull, in.area, ',');
        getline(SQLPull, in.toolset, ',');
        getline(SQLPull, in.date);
        qef.push_back(in);
    }
    SQLPull.close();

    for (const auto &q: qef)
        cout << q << '\n';

    return 0;
}

Last edited on
Thank you! I must of missed that line when I copied stuff over in sections.

So, to see if I understand what you did there. Instead of a class, you made a structure and are pulling each piece of data into the structure with the while loop.

The "vector <QEF> qef", is making a vector of QEF structures, which has all the data attached to each one.

qef.push_back(in) is putting the last QEF structure that was made onto the end of the vector QEF, essentially making a stack of QEF structures that I can manipulate.


What does this do, and why is it needed :
1
2
3
4
ostream& operator<<(ostream& os, const QEF& q) {
    return os << q.number  << ':' << q.title << ':' << 
                 q.entity  << ':' << q.area  << ':' << 
                 q.toolset << ':' << q.date;


And this section i'm assuming is an output, but I dont understand the "const auto".
1
2
for (const auto &q : qef)
		[cout << q << '\n';



Again, thanks so much for the help and response.
Last edited on
overloading stream operators lets you do the usual stream operations in a single line of code, useful for file read, write, and cin/cout statements etc.

study auto. Autos infer their type from the code rather than explicitly, for example

auto I = 1; //I is an int!
auto d = 3.14; //d is a double!
and so on. Honestly, it has its place, but its also unreadable when overused. Maybe I am still just new at it, but I find the auto : for loops take too much energy to read when studying others' code. Its easier to write, but other gibberish shortcuts are frowned upon while this one is embraced.

const is constant … the 'variable' isn't a variable anymore, it can't be changed.

const double pi = 3.14; //ok
pi = 5.0; //compiler error, its a constant and you tried to change it!

here, its a constant reference, by the way.
Last edited on
Topic archived. No new replies allowed.