Search
 
SCRIPT & CODE EXAMPLE
 

CPP

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
";
 
	for(int i=0;i<content.size();i++)
	{
		for(int j=0;j<content[i].size();j++)
		{
			cout<<content[i][j]<<" ";
		}
		cout<<"
";
	}
 
	return 0;
}
 





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

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
";
 
	for(int i=0;i<content.size();i++)
	{
		for(int j=0;j<content[i].size();j++)
		{
			cout<<content[i][j]<<" ";
		}
		cout<<"
";
	}
 
	return 0;
}
 





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

PREVIOUS NEXT
Code Example
Cpp :: priority queue ordered by second element 
Cpp :: how to shut down windows in c++ 
Cpp :: compute the average of an array c++ 
Cpp :: c++ ros subscriber 
Cpp :: you wanna import math on c++ 
Cpp :: check if double is integer c++ 
Cpp :: how to iterate in string in c++ 
Cpp :: how to get mouse position on window sfm; 
Cpp :: inreament operator 
Cpp :: how to use dec in C++ 
Cpp :: retourner pointeur de type qstringlist qt 
Cpp :: cannot open include file unreal 
Cpp :: cpp iterate words of string 
Cpp :: cuda constant memory initialisation 
Cpp :: c++ chrono 
Cpp :: differency between c++ std and stl 
Cpp :: write variable to file cpp 
Cpp :: remove () not working c++ 
Cpp :: check if c++ is installed 
Cpp :: how to declrae an array of size 1 
Cpp :: using find in vector c++ 
Cpp :: how to check sqrt of number is integer c++ 
Cpp :: C++ mutex lock/unlock 
Cpp :: c++ initialize array with all zeros 
Cpp :: c++ memory leak 
Cpp :: check if float has decimals c++ 
Cpp :: cpp macro 
Cpp :: use ::begin(WiFiClient, url) 
Cpp :: length of string c++ 
Cpp :: how print fload wiht 3 decimal in c++ 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =