3

Hello I am wondering what the most efficient way would be to read a .csv file and store each comma separated item into its own array or variable. I can only use #include <iostream>. I was thinking about maybe using .getLine(), but all i would know is the delimeter and value to store since there is no <fstream> allowed. That being said does anyone know how I could go about this? Thanks in advance!

This is the layout of the file. The first line indicates the umber of rows to be read.

input file

3
2014,Computer Science,Utah,1568,44.9
2014,Marketing,Michigan,23745,983
215,Business Management, Idaho,256,674

code:

int year;
char* major = new char[40];//cant be longer than 40 characters
char* state = new char[40]; 
int enrolled;
int rate;
char p;//for cin.get characters possibly

cin>>rows

for(int i = 0; i <= rows; i++){

HMMMMM?

}
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
snipshow7
  • 87
  • 1
  • 8
  • Did you try using `getline`? It would seem to be a great start to a solution. You would not need to `cin` as you would the number from the file. – Paul Rooney Feb 13 '18 at 06:50
  • I still don’t get the dumb restrictions professors tend to put on assignments like these (use the stream library, but hey, not to its full power, LOL, suckers). – Dúthomhas Feb 13 '18 at 07:08
  • 1
    @snipshow7, “efficient” is not the word you should care about. First, you’ll need an array (or better yet, a _vector_) to store each record, which should be its own class. Write a function to read a single record. Write a function that uses the first function to read all records. – Dúthomhas Feb 13 '18 at 07:10
  • Should link straight to https://stackoverflow.com/questions/1120140/how-can-i-read-and-parse-csv-files-in-c as duplicate. – Dúthomhas Feb 13 '18 at 07:10
  • Read a file, no `fstream` allowed. I guess that leaves piping the file to `cin` or using the C file handling functions. I wonder is `fread`ing into a `std::stringstream`, and then parsing the `stringstream` would be breaking the rules. – user4581301 Feb 13 '18 at 07:27
  • Cant use a vector. Yes I was thinking of getLine(). But what could i do for the stream size int getLine(char* s, streamsize n, char delim). – snipshow7 Feb 13 '18 at 18:06

1 Answers1

1

You can use cin.getline() function which also accepts a delimiter. As for storing the entire csv record, you can define a structure which stores all the fields.

#include <iostream>
#include <cstring>
using namespace std;

typedef struct temp_struct {
    char major[40];
    char state[40];
    float rate;
    int year;
    int enrolled;
} RECORD;

int main() {
    int rows;
    cin >> rows;
    RECORD r[rows];
    char temp[100];
    for(int i = 0; i < rows; i++) {
        cin.getline(temp, 100, ',');
        r[i].year = atoi(temp);

        cin.getline(temp, 100, ',');
        strcpy(r[i].major, temp);

        cin.getline(temp, 100, ',');
        strcpy(r[i].state,temp);

        cin.getline(temp, 100, ',');
        r[i].enrolled = atoi(temp);

        cin.getline(temp, 100);
        r[i].rate = atof(temp);

        cout << r[i].year << " " << r[i].major << " " << r[i].state << " ";
        cout << r[i].enrolled << " " << r[i].rate << endl;
    }
    return 0;
}

For delimiters, the first four delimiters would be , and since the last value rate has no , following it rather there's a \n, no delimiter is specified in the cin.getline() (since default delimiter is \n itself).

I have tested it; here's the link https://ideone.com/ycht1b

kiner_shah
  • 3,939
  • 7
  • 23
  • 37
  • I was thinking of doing it this way but aren't you using #include ? – snipshow7 Feb 13 '18 at 18:02
  • It also has to all be done in main. So would I be able to use that "temp_struct" a different way in main? – snipshow7 Feb 13 '18 at 18:03
  • @snipshow7, You may implement your own functions for `atoi` and `atof` (which are present in `cstring`), I think it would be a good exercise for you. Also, you can use arrays instead of structures. But, I think structures are better since it's more convenient to use them (at least to me :-P). – kiner_shah Feb 15 '18 at 04:22
  • but isn't cString a #include header? – snipshow7 Feb 16 '18 at 02:40
  • I meant to say you can avoid using `cstring` for `atoi` and `atof`. You just need to implement your own functions for converting `char*` to `int` and `char*` to `float`. – kiner_shah Feb 17 '18 at 06:06