[HELP] Im reading data from .csv and storing into arrays

If someone who knows this fairly well, id like to help along the way of doing this little project i have.

My boss at work generates a .csv file for call logs and im trying to build a program that will read the file, store data, and manipulate data as i need to and then display data.

I started the code but im hung up on a couple parts.

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
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
    //opens the csv file.
    ifstream gwfile;
    gwfile.open("log.csv");

    if(!gwfile) { // file couldn't be opened
      cout << "FAILED: file could not be opened" << endl << "Press enter to close.";
      cin.get();
      return 0;
    }else
        cout << "SUCCESSFULLY opened file!\n";

    cout << "-------------------------------------------------------------------------------\n\n\n";

    long int SIZE = 0;
    char data[SIZE];
    cout << "This is data SIZE:" << data[SIZE] << endl;

    //in the csv im trying to only read lines that start with the voice as those are only valid data we need.
    //also i would like to display the headings in teh very first line
    while( !gwfile.eof() ){
        //This is where im trying to only accept the lines starting with "Voice"
        if(data[SIZE] == "Voice"){
            for(int i=0; i!='\n';i++;){
            }
        }
//        getline(gwfile, data, '');
//        cout << data[0];
    }
    return 0;
}
No one knows? lol
To read a file line by line, you can use
1
2
3
4
5
string line; // Also need to have #include <string> for this
while (!gwfile.eof())
{
    getline(gwfile, line);
    // Search "line" for what you're looking for 
I'd use std::getline to read the entire line into a std::string. The parse the string by putting it into an ostringstream and extracting with getline delimited by ','

If the csv uses quotes to contain strings with non-delimiter commas you'll have to deal with that as well.
Within the lines there will be pieces of data that in an excell spread sheet it would be columns im adding up. With this be possible by storing them into strings?

What im thinking is i would need to store the data into arrays per each line and then adding up each element per array.

example:
csv would have

Fred,25,home,555-5555,current //this would be line 1
,36,work, 555-5555, current //this is line 2 but since the name field is blank i would like to skip this line.

any ideas? im kinda new to programming
As others have suggested.

Read an entire line.
Parse that line.
Apply "your" business logic based on what was read in, if you need to skip than skip, if you need to put what you've read into some container do that...

This post should help.

http://www.cplusplus.com/forum/general/17771/
Topic archived. No new replies allowed.