Read the CSV file and assign it to an array arr [100]

New member...

Hi everybody. I think this is a problem many friends like me will face. Thanks to everyone for listing ways to handle it.

The CSV file has 6 data cells per line, type string string string double double.
Except the first line is string.
The solution to the problem of the request is to use the function to cut the string and then set the initial value again.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

Employee arr[N];

const vector<string> explode(const string& s, const char& c) {
    string buff { "" };
    vector<string> v;

    for (auto n : s) {
        if (n != c)
            buff += n;
        else if (n == c && buff != "") {
            v.push_back(buff);
            buff = "";
        }
    }
    if (buff != "")
        v.push_back(buff);

    return v;
}


1. So how do you set the value for the first line to the array? It always reported an error.
2. Why not write a separate title so you don't have to deal with it?

Thank for your time! ^_^
Last edited on
Please post a small sample of your input file.

What does the code you posted have to do with reading a CSV file?

> So how do you set the value for the first line to the array? It always reported an error.
iirc, the first line does not contain the data you want, so you would not put it on the array
also, if your code gives you an error, then post the code that gives you the error and copy-paste the error message

> Why not write a separate title so you don't have to deal with it?
I don't understand what you mean, please provide an example.
1. A small sample of my input file



FirstName, LastName, Job, Department, AnnualSalary, EstimatedSalary
a1, a2, a3, a4, 1, 1
b1, b2, b3, b4, 2, 2
c1, c2, c3, c4, 3, 3





2. A part of my code. It worked.
But my teacher said

Instead:
const vector<string> explode(const string& s, const char& c)

I should create a function:
Employee& explode(const string& s, const char& c).
I don't understand?

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42

const int N = 100;
Employee employees[N]; // Employee is a class

void ReadToArray(Employee arr[], string filename) 
{
	int i = 0;
	ifstream file(filename);
	if (file.is_open())
	{
		string line;
		getline(file, line, '\n'); // Mean skip the first line? Right?
		while (getline(file, line))
		{
			vector<string> v = explode(line, ',');
			arr[i].setFirstName(v[0]);
			arr[i].setLastName(v[1]);
			arr[i].setJobTitle(v[2]);
			arr[i].setDepartment(v[3]);
			arr[i].setAnnualSalary(stod(v[4]));
			arr[i].setEstimatedSalary(stod(v[5]));
			i++;
		}
	}
}

void Display(Employee arr[], int size) 
{
	for (int i = 0; i < size; i++) 
	{
		employees[i].printData();
	}
}



int main() 
{
	ReadToArray(employees, "Employees.csv");
	Menu();
	return 0;
}


----------------------------------------------------------

@ne555
> Why not write a separate title so you don't have to deal with it?

I mean, in CSV forms only the content should be written, not the title.

Such as:
a, a, a, a, 1, 1
b, b, b, b, 2, 2

The content should not be written as:

title1, title2, title3, title4, title5, title6
a, a, a, a, 1, 1
b, b, b, b, 2, 2

Because we have to "cout" the title ourselves, right? We always have to skip line 1. Is this inconvenience necessary?

Thank for your time! :)
Last edited on
We always have to skip line 1. Is this inconvenience necessary?

Unless you want to use the "names" of the fields in some output just retrieving and discarding the header line is acceptable.


I don't understand?

What don't you understand? I appears to me that your instructor is suggesting that instead of returning the std::vector<string> that you return a reference to an instance of a Employee. However I would suggest just returning an instance of the Employee not a reference.

Also have you considered using stringstreams in your "explode" function and use combinations of getline() and the extraction operator to parse the lines?



We always have to skip line 1. Is this inconvenience necessary?
inconvenience? I don't see your problem here. You already skipped the line?

Normally in real world code the header line is one of the most important lines. It is the opportunity to check whether the content of the file is sane because unlike the following lines you know exactly what it is.

I should create a function:
Employee& explode(const string& s, const char& c).
This would reduce the code to:
1
2
3
4
5
		while (getline(file, line))
		{
			arr[i] = explode(line, ',');
			i++;
		}
While I would think that he means

Employee explode(const string& s, const char& c) // Note: Without &
Topic archived. No new replies allowed.