Read data from .CSV and store in different variables

Hi, I am a mechanical engineer, and I've learnt the basics of VBA (excel's macro) and C++ in the past week, because I have to programm a simple C++ model.
The model has to read some input data from a .csv file (the measurements), elaborate the data and write the results in another .csv file...

The problem I am facing now is that the .csv file is made by 27 variables, measured each second (so it's made by 27 columns and "n" rows, where n is a number between 300 and 800, depending on the lenght of the test). The first row is made by headers, so I want to skip it, and the last column has no coma at the end, but a end-of-line "\n".
I want to assing each of the 27 values read to a variable (double precision), do the calculations, and then go to the next line and do it again until the end of the file...
I am currently using the following code (which is not working correctly):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    char filename[64];
    string inputs;
    double input_array[26];
    cout<<"File to read>";
    cin.getline(filename, 64);
    ifstream input_data(filename);
    if (input_data.is_open())
    {
        input_data.seekg (1, ios::beg);
        while(!input_data.eof())
        {
            for(int i=0; i<27; i++)
            {
                getline (input_data, inputs, ',');
                Valid_Calculation = 1;
                istringstream iss (inputs,istringstream::in);
                iss>>input_array[i];
            }
        //... model calculations    
        }
    }


It asks for the name of the file with the scans, opens it and I was trying to use the input_data.seekg command to skip the first line, but it's quite difficult to get the exact number of characters to skip!!!
The for cycle is aimed to store the values read into an array, so that i have separated each variable from the following.


The problem are:
- at the end of each row there isn't a coma, so the getline doesn't separate the last value of a row from the first of the following row.
- I don't know how to skip the first row made by headers (text).
- saving to the array doesn't seem to work, but maybe it's because of the previous problems.


Thank you!
Last edited on
To skip the first line, you can just use std::getline() with the default delimiter ('\n').

To skip the first row, you could read the entire line with getline(), then use members of std::string, .find() and .substr() to get the parts you want.

I would also suggest changing your char*filename to std::string, so you can more easily read/write data.
Great! I can now jump directly to the second row... :)

Any clue on how to separate the last value of a row from the first of the following row? I have found this:

1
2
3
#define eob 0x0d
while (!eob=0)
...


This should define eob as "\n", and then I should create two separate while loops to get different strings... But I'm not sure!

Thanks 4 the reply! ;)
Last edited on
If you are using getline() and substr(), you won't have to, since the end of the string will be the end of the line, and the next time you use getine(), you will get the next line.
Yes!

It's working... Thank you!

Now I have to fix the model, because it crashes during the loops and the calculations...

But the data input part is working correctly!!

Thank you... ;)
Topic archived. No new replies allowed.