I am asked as part of a homework to load data from an excel file
Does anyone know how to do that?
I can't find anything of the sort online..
(Excel file is in this form:
name1; ID1
name2; ID2
name3; ID3
)
I am asked as part of a homework to load data from an excel file
Does anyone know how to do that?
I can't find anything of the sort online..
(Excel file is in this form:
name1; ID1
name2; ID2
name3; ID3
)
You seem to not have done any effort. Just seeing a C++ course on "how to read a file in C++" would give you the answer.
And I cannot believe that you found nothing online, except if you never searched anything.
You will see below an example of how to read a file that have the format you specified (.csv I guess). But it does not handle the cases your data file is corrupted.
And... I will not explain the code, I think you have to do the effort to search by yourself online a C++ course or C++ documentation that will explain you the instructions I used to fully understand what the code does.
#include <fstream>
#include <iostream>
#include <vector>
std::vector<std::string> split(const std::string & s, char c);
int main()
{
std::string file_path("data.csv"); // I assumed you have that kind of file
std::ifstream in_s(file_path);
std::vector <std::pair<std::string, std::string>> content;
if(in_s)
{
std::string line;
while(getline(in_s, line))
{
std::vector<std::string> splitted(split(line, ';'));
while(splitted[1][0] == ' ')
splitted[1].erase(0, 1); // remove the white(s) space(s)
content.push_back(std::make_pair(splitted[0], splitted[1]));
}
in_s.close();
}
else
std::cout << "Could not open: " + file_path << std::endl;
for(std::pair<std::string, std::string> line : content)
std::cout << line.first << "; " << line.second << std::endl;
return 0;
}
std::vector<std::string> split(const std::string & s, char c)
{
std::vector<std::string> splitted;
std::string word;
for(char ch : s)
{
if((ch == c) && (!word.empty()))
{
splitted.push_back(word);
word.clear();
}
else
word += ch;
}
if(!word.empty())
splitted.push_back(word);
return splitted;
}
Good luck !