.
.
        string line;
	vector<string>DataStore;

	ifstream file;
	file.open(fileName.c_str());

	if (!file.is_open()){
		cout<<"Error retrieving file " <<fileName<<endl;
		
	}

	while(getline(file,line))
	{
		getline(file,line);
		stringstream lineStream(line);
		string bit;

		getline(lineStream, bit, ',');
		getline(lineStream, bit, '\n');
		DataStore.push_back(bit);  
.
.

This piece of code takes a CSV and stores its data in a vector. The first column of the CSV contains a mix of numbers and words, the second column of the CSV contains numerical data only. How can I store the data from column one only?

Any help appreciated,
Thanks!

Recommended Answers

All 8 Replies

In your while() loop condition that actually pulls a line out of the file and stores it into line so you are reading every second line with your code. Also you are overwriting your holding variable bit with the 2nd column of information.

The only part that is really different below is the while() loop. Other than that I think its pretty much the same.

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main()
{
	string line;
	vector<string> DataStore;

	ifstream file("test.txt");

	while( getline(file,line, ',') )
	{
		DataStore.push_back(line);
		getline(file,line);
	}

	for( unsigned int i = 0; i < DataStore.size(); i++ )
		cout << DataStore[i] << endl;

    return 0;
}

Thanks for the reply sfuo. The problem is, it only stores the first entry of the column.

Is that not what you asked above?

How can I store the data from column one only?

Ah no, sorry. The original CSV has two columns, the first is a column of numbers and letters and the second a column of numbers. There are several entries in each column, I was looking to get the entire first column into the vector.

Looking at the quote, it does look a bit misleading haha. I meant column #1.

Sorry about that.

Can you post a sample input? I cant visualize this at all.

This is what I'm seeing

abc123,12345
def456,94032
....

Sat Aug 11 22:00:00 BST 2001, 8.231111104
Sat Aug 11 23:00:00 BST 2001, 7.71666666
Sun Aug 12 00:00:00 BST 2001, 8.231111104

Something like this.

I was wondering why you called it CSV since there wasnt a comma in it. Then I refreshed the page a few times and you edited your post! I have no idea how you were kinda thinking you wanted the data to be stored so I made a DATA structure to hold the strings and numbers.

I'm sure you can think up a better way to output the data if you wanted to.

#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>

using namespace std;

struct DATA
{
	string day, month;
	int date;
	string time, timezone;
	int year;
	void Display()
	{
		cout << day << " " << month << " " << date << " " << time << " " << timezone << " " << year;
	}
};

int main()
{
	string line;
	vector<DATA> DataStore;

	ifstream file("test.txt");

	while( getline(file,line, ',') )
	{
		stringstream lineStream(line);
		DATA element;
		lineStream >> element.day >> element.month >> element.date >> element.time >> element.timezone >> element.year;

		DataStore.push_back(element);
		getline(file,line);
	}

	for( unsigned int i = 0; i < DataStore.size(); i++ )
	{
		DataStore[i].Display();
		cout << endl;
	}

	file.close();

    return 0;
}

That is a HUGE help, thanks a lot!

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.