Array load from csv and malloc

Hello, so i must load some data from csv and put it into two dimension array and i am complete lost. If i have some csv file like:
Example: 
1997,Ford,E350,2.34
2000,Mercury,Cougar,2.38


and i need to get it into array on position
0,0 = 1997
0,1 = Ford
0,2 = E350
0,3 = 2.34
1,0 = 2000
1,1 = Mercury

etc.

This is what i try to do, i know how it should works but i have no idea how i should write code. Probebly i must replace X, X should be size of actual line to read to malloc it right. And also i must replace Y, this should be number of lines in csv file. Can anybody help me please ? :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    case 1:
    {   
    char *line = (char*)malloc(X*sizeof(char));
    ifstream number1;
    number1.open("files/cars.csv");
    if (number1.is_open())
    {
    for(int i = 1; i <= Y;i++)
        {
        number1.getline (line,X);
        cout << line << '\n';
        }
     number1.close();
    }

    return 0;
    break;
    }
bump
In c++ usually memory is allocated using the new operator.
However, in this case, I'm not sure that it is even necessary. You could simply define line with a fixed size of say 100, that would probably he adequate. However, it would be better to just use a std::string which would take care of the memory allocation for you.

I thought i saw a previous thread where the same question was already answered to your satisfaction, so I'm not really sure what it is you want to know here. But still, I'll offer two different solutions to this.
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
43
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <sstream>

    using namespace std;

int main()
{
    const int width = 4;

    string array[20][width];
    int rows = 0;
    ifstream number1("files/cars.csv");
    if (number1.is_open())
    {
        string line, word;

        while (getline(number1, line))
        {
            istringstream ss(line);
            int cols = 0;
            while (getline(ss, word, ',') )
            {
                array[rows][cols] = word;
                cols++;
            }
            rows++;
        }
    }

    for (int i=0; i<rows; i++)
    {
        for (int j=0; j<width; j++)
        {
            cout << setw(10) << array[i][j];
        }
        cout << endl;
    }

    return 0;
}

Last edited on
Second version. Instead of a 2D array, I've used a struct to hold the data for each car. A single dimension array of this object would do, but here I used a vector as it means there's no need to specify the size.
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
#include <cstdlib>

    using namespace std;

struct vehicle {
    int year;
    string make;
    string model;
    double size;
};

int main()
{
    vector<vehicle> cars;

    ifstream number1("files/cars.csv");
    if (number1.is_open())
    {
        string line, word;

        while (getline(number1, line))
        {
            vehicle car;
            istringstream ss(line);
            int cols = 0;

            while (getline(ss, word, ',') )
            {
                switch(cols)
                {
                    case 0:
                        car.year = atoi(word.c_str());
                        break;
                    case 1:
                        car.make = word;
                        break;
                    case 2:
                        car.model = word;
                        break;
                    case 3:
                        car.size = atof(word.c_str());
                        break;
                }
                cols++;
            }
            cars.push_back(car);
        }
    }

    cout << left;
    for (unsigned int i=0; i<cars.size(); i++)
    {
        cout << setw(5)  << cars[i].year
             << setw(10) << cars[i].make
             << setw(10) << cars[i].model
             << setw(5)  << cars[i].size
             << endl;
    }

    return 0;
}
Thank you again Chervil :)
Topic archived. No new replies allowed.