Horje
cpp read csv Code Example
cpp read csv

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
 
using namespace std;
 
int main()
{
	string fname;
	cout<<"Enter the file name: ";
	cin>>fname;
 
	vector<vector<string>> content;
	vector<string> row;
	string line, word;
 
	fstream file (fname, ios::in);
	if(file.is_open())
	{
		while(getline(file, line))
		{
			row.clear();
 
			stringstream str(line);
 
			while(getline(str, word, ','))
				row.push_back(word);
			content.push_back(row);
		}
	}
	else
		cout<<"Could not open the file\n";
 
	for(int i=0;i<content.size();i++)
	{
		for(int j=0;j<content[i].size();j++)
		{
			cout<<content[i][j]<<" ";
		}
		cout<<"\n";
	}
 
	return 0;
}
 





/* Output
 
Enter the file name: sample.csv
name gender age 
abc male 21 
xyz female 18 
pqr male 19 
*/




Cpp

Related
SFML texture from file max size Code Example SFML texture from file max size Code Example
Get input for array of unknown length/size in c++ Code Example Get input for array of unknown length/size in c++ Code Example
command loop ctrl D c++ Code Example command loop ctrl D c++ Code Example
what is xor_eq c++ Code Example what is xor_eq c++ Code Example
Check whether the jth object is in the subset Code Example Check whether the jth object is in the subset Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
8