0

I want to read a excel file in to a structure.But the thing is it reads the excel columns as whole line.I need those data in columns one by one.Using fstreams I have a file opened that contains numerous columns

enter image description here

I tried it with the knowledge that I have.But It displays all the details as a whole column as i've shown below

Name :      Name,Nick
Nick :   name,Phone
Phone :  number,Carrier,Address
Carrier :   Yashodhara,Yash,711256677,Mobitel,"No.
Address : 29,Bollatha,Ganemulla"
-----------------------
Name :      Madushani,Madu,711345678,Mobitel,"No.
Nick :   12,
Phone :  Gampaha"
Carrier :   Sadeepa,Sad,789002264,Hutch,"No.
Address : 123,
-----------------------

I tried this with the following code.

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;

//structure for store contact details
struct contacts {
    string name;
    string nickName;
    string phoneNumber;
    string carrier;
    string address;
};




int main() {
    
    const int LIMIT = 10;
    contacts limit[LIMIT];
    ifstream file;
    file.open("Contact.csv");

    if (file) {
        while (!(file.eof())) {
            for (int i = 0; i <= 7; i++) {
                file >> limit[i].name;
                file >> limit[i].nickName;
                file >> limit[i].phoneNumber;
                file >> limit[i].carrier;
                file >> limit[i].address;
                
            }
        }
    }
    else {
        cout << "Error";
    }

    // Using the following to debug output
    for (int i = 0; i < 7; i++) {
        cout << "Name :      " << limit[i].name << endl
            << "Nick :   " << limit[i].nickName << endl
            << "Phone :  " << limit[i].phoneNumber << endl
            << "Carrier :   " << limit[i].carrier << endl
            << "Address : "  << limit[i].address << endl
            << "-----------------------" << endl;
    }


    return 0;
}

I want to know how to read above details in to the structure in the proper order.

1 Answers1

0

You can use std::getline()

string readString;
if (file) {

    while (!(file.eof())) {
        getline(file,readString); // Read the column headers
        for (int i = 0; i <= 7; i++) {
            getline(file,limit[i].name,','); // ',' is the separator
            getline(file,limit[i].nickName,',');
            getline(file,limit[i].phoneNumber,',');
            getline(file,limit[i].carrier,',');
            getline(file,limit[i].address); // Read until the end of the line
            
        }
    }
}

I have to mention that I didn't test it so it might need some arrangements.

Wai Ha Lee
  • 7,664
  • 52
  • 54
  • 80
Tom Rivoire
  • 284
  • 4
  • 3
    [Why !.eof() inside a loop condition is always wrong.](https://stackoverflow.com/q/5605125/9254539) (and you can't use `getline()` correctly without checking the stream-state following each call) E.g `if (!getline(file,limit[i].name,',')) { // handle error }` – David C. Rankin Aug 14 '20 at 06:36