reading and using data in a .csv file

I want to write a program that takes in a .csv file with over 2000 columns on information but i have no idea how to and so far i have:

1
2
3
4
5
6
7
8
9
10
11
int main() {
	ifstream theFile (“example.csv”);
	
	string dateTime;
	double price;
	string name;
	
	while (theFile >> dateTime >> price >> name) {
		
	}
}


what i want my final program to do is show a menu to the user to (a) see all the data in highest to lowest price and (b)cheapest price and (c) exit. I know all about menus but i can't figure out how to get my program to use the data in my .csv file? thank you!!
csv is a simple line by line format:
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
#include <string>
#include <sstream>
#include <vector>
#include <fstream>

int main()
{
  std::ifstream theFile ("example.csv");

// ...

  std::string line;
  std::vector<std::vector<std::string> > values;
  while(std::getline(theFile, line))
  {
    std::string line_value;
    std::vector<std::string> line_values;
    std::stringstream ss(line);
    while(std::getline(ss, line_value, ';'))
    {
      line_values.push_back(line_value);
    }
    values.emplace_back(line_values);
  }

  return 0;
}
It reads the data into a 2 dimensional vector. Note that it is not tested
Thanks but I have a question. If my .csv file that contained data such as:

1
2
3
4
5
6
Name	        City	         Time of Birth
Jack Daniels	London	 10/10/1976 6:10:00 am
Tim Adam	        Denmark	 2/11/1988 5:3:00 am
A.J Peters	        Lagos	 10/02/1994 14:22:00 pm
Peeta Vijay	Boston       03/02/1994  15:22:00 pm
Andre Gomez	Havana	 10/09/1980 16:57:00 pm


would it automatically disregard the header "Name, city and Time of birth"? or should I remove them from the .csv file myself?

Also, how would someone who is using my program on another computer be able to access the .csv file? would they have to have the same exact file in their computer as well? with the same address?
would it automatically disregard the header "Name, city and Time of birth"?
Nothing happens automatically. Add another getline before the while loop in order to dismiss the header.

A csv file follows the same rules as any other file. Usually you cannot access files on other computers if they don't share directories or something alike.

with the same address?
What address?
Okay, thanks.

Would it be too much trouble for you to explain to me exactly what every line in the code you gave me does? I'm a bit confused by it. thanks again

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
#include <string>
#include <sstream>
#include <vector>
#include <fstream>

int main()
{
  std::ifstream theFile ("example.csv");

// ...

  std::string line;
  std::vector<std::vector<std::string> > values;
  while(std::getline(theFile, line))
  {
    std::string line_value;
    std::vector<std::string> line_values;
    std::stringstream ss(line);
    while(std::getline(ss, line_value, ';'))
    {
      line_values.push_back(line_value);
    }
    values.emplace_back(line_values);
  }

  return 0;
}
Here is the descripton for
getline:
http://www.cplusplus.com/reference/string/string/getline/?kw=getline
vector:
http://www.cplusplus.com/reference/vector/vector/?kw=vector
stringstream:
http://www.cplusplus.com/reference/sstream/stringstream/?kw=stringstream


The reason why this:
1
2
3
	while (theFile >> dateTime >> price >> name) {
		
	}
doesn't work is that it would require whitespace as delimiter, but there are actually semicolons in a csv

To discart the first line:
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
#include <string>
#include <sstream>
#include <vector>
#include <fstream>

int main()
{
  std::ifstream theFile ("example.csv");

// ...

  std::string line;
  std::vector<std::vector<std::string> > values;
  std::getline(theFile, line); // Note
  while(std::getline(theFile, line))
  {
    std::string line_value;
    std::vector<std::string> line_values;
    std::stringstream ss(line);
    while(std::getline(ss, line_value, ';'))
    {
      line_values.push_back(line_value);
    }
    values.emplace_back(line_values);
  }

  return 0;
}


If my .csv file that contained data such as
If it is all whitespace you can actually use >>
Topic archived. No new replies allowed.