CSV importing and converting to intergers

Hello Everyone! I'm a beginner in c++ and i'm trying to import a csv file and use the data it has to perform some operations. The file in question is fairly large and has 2800+ rows and 3 columns. I want to ignore the first row and column since they're only labels and store the numbers(as integers) in the last two columns in arrays. The only code i have typed so far is really only:

 
  inFile.open("/Users/THEN1203/Desktop/example.csv");


Thank You!
Last edited on
The only code i have typed so far is really only:


Since that is the only code you typed. You may want to look at the file tutorials:

http://www.cplusplus.com/doc/tutorial/files/

Once you can type more than inFile.open("/Users/THEN1203/Desktop/example.csv"); We can help you better.
@chicofeo after some sleep and some googling i found out a way to do it but when i run the code it doesn't read in the last column for some reason. It's just a bunch of zeros.

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
int main()
{
    int Geneinfo[2823][3];
    string item;
    ifstream myfile;
    myfile.open("/Users/T_HEN1203/Desktop/Dark Aerobic.csv");
    if (myfile.is_open())
    {
       // for loop for converting the strings into ints and storing them in a multidimensional array
       for (int row(0); row < 2823; ++row)
       {
           getline(myfile, item);
           stringstream iss(item);
           for (int col(0); col < 3; ++col)
           {
                string line;
                getline(iss, line, ',');
                // used to break loop if files stops being read
                if ( !iss.good() )
                {
                    break;
                }
                stringstream convertor(line);
                convertor >> Geneinfo[row][col];
           }
       }
       // used to display the array
       for (int r = 0; r < 2823; ++r)
       {
           for (int c = 0; c < 3; ++c)
           {
               cout << Geneinfo[r][c] <<" ";
           }
           cout << endl;
       }
    }
    else
    {
        cout<<"The file isnt open.\n";
    }
    return 0;
}

I want to ignore the first row

I don't see where you're ignoring the first row.

Please show us the format of your file.
The header row and the first couple of data rows is sufficient.


Data Example:
Name 1, Name 2, Name 3
641610012, 1350, 17
659855655, 569, 89
142664525, 589, 122


I was thinking it might be beneficial to keep it. I was gonna try to reconvert it and use it again but they (names) also gets stored as zeros. Would the spaces in the names have something to do with that?
Here is the most recent version:
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
int main(){

    double sumGCL, TPM, FPKM, sumCNTS;
    string item;
    ifstream myfile;
    myfile.open("/Users/T_HEN1203/Desktop/DarkAerobic.csv");
    //used to count the number of lines in the file
    int numLines(0);
    string unused;
    while ( getline(myfile, unused) ){
        ++numLines;
    }
    //varibles to hold info from file
    int Geneinfo[numLines][3];
    int Gene_ID[numLines];
    float Gene_length[numLines], Gene_RC[numLines];
    if ( myfile.is_open() ){

        // for loop for converting the strings into ints and storing them in a multidimensional array
        for (int row = 1; row < numLines; ++row){
            getline(myfile, item);
            stringstream iss(item);

            for (int col(0); col < 3; ++col){
                string line;
                getline(iss, line, ',');

                // used to break loop if files stops being read
                if ( !iss.good() ){
                    break;
                }
                stringstream convertor(line);
                convertor >> Geneinfo[row][col];
           }
        }


        // used to display the array
        cout << "Gene ID " << "Length " << "Read Counts";
        for (int r = 0; r < numLines; ++r){

            for (int c = 0; c < 3; ++c){
               cout << Geneinfo[r][c] <<" ";
            }

            cout << endl;
        }
       
    }
    else{
        cerr<<"ERROR: The file isnt open.\n";
    }
    return 0;
}
There are several problems with your code.

First array sizes in C++ must be compile time constants. If you want user sized arrays you should be using std::vector instead. The other alternative would be to use dynamic memory, new/delete.

1
2
3
4
    //varibles to hold info from file
    int Geneinfo[numLines][3];
    int Gene_ID[numLines];
    float Gene_length[numLines], Gene_RC[numLines];

Since numLines is not a compile time constant, all of the above lines should produce error messages.

Second your first loop reads to the end of the file which sets an error flag. Which means until you reset the error flag none of the rest of the file reads will succeed.

Thank you @jlb that did the trick!
Topic archived. No new replies allowed.