0

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

)

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
Alex Gpy
  • 1
  • 1
  • 1
  • 5
    Homework questions (more than other questions) should contain a summary of what you tried and what is missing. Also, If you didnt find something online then you didnt really search – 463035818_is_not_an_ai May 13 '19 at 13:10
  • 2
    Possible duplicate of [How can I read and parse CSV files in C++?](https://stackoverflow.com/questions/1120140/how-can-i-read-and-parse-csv-files-in-c) – Clonk May 13 '19 at 13:18
  • BTW, the description you give is not for an Excel file because they are binary files, but the description of a CSV file. A real parser is rather hard because the full spec is complex but here you just need to read the file line by line, and split each line on the `;` separator. Try something and ask for more if you get stuck. – Serge Ballesta May 13 '19 at 13:20
  • I understand the confusion, because Microsoft Office forces a ton of file extensions to be read by it's programs by default. But in fact, `.csv` is a simple text file representing a table, where one expects text data in a specified format: columns separated by a character (usually `,`, `;`), and rows separated by `\n` (so each row in separate line). – Yksisarvinen May 13 '19 at 13:31

1 Answers1

1

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 !

Fareanor
  • 5,900
  • 2
  • 11
  • 37