Lcr
Lcr

Reputation: 75

c++ get array string from text file

my text file was like

Jason Derulo
91 Western Road,xxxx,xxxx
1000
david beckham
91 Western Road,xxxx,xxxx
1000

i'm trying to get the data from a text file and save it into arrays however when i want to store the data from the text file into array it loop non-stop. what should i do ? the problem exiting in looping or the method i get the data from text file ?

code:

#include <iostream>
#include <fstream>

using namespace std;

typedef struct {

    char name[30];
    char address[50];
    double balance;

} ACCOUNT;

//function prototype
void menu();
void read_data(ACCOUNT record[]);

int main() {
    ACCOUNT record[31]; //Define array 'record'  which have maximum size of 30
    read_data(record);  
}
//--------------------------------------------------------------------

void read_data(ACCOUNT record[]) {
    ifstream openfile("list.txt");              //open text file 

    if (!openfile) {
        cout << "Error opening input file\n";
        return 0;
    } else {
        int loop = -1;                  //size of array 
        cout << "--------------Data From File--------------"<<endl;
        while (!openfile.eof())  {
        if (openfile.peek() == '\n') 
            openfile.ignore(256, '\n');
        openfile.getline(record[loop].name, 30);
        openfile.getline(record[loop].address, 50);
        openfile >> record[loop].balance;
        }
        openfile.close();               //close text file

        for (int i = 0; i <= loop + 1; i++) {
            cout << "Account "  << endl;
            cout << "Name         : " << record[i].name << endl;
            cout << "Address      : " << record[i].address << endl;
            cout << "Balance      : " << record[i].balance << endl;
        }
    }
}

Upvotes: 2

Views: 1485

Answers (1)

jrd1
jrd1

Reputation: 10726

Use ifstream::getline() instead of ifstream::eof() in tandem with >>. The following is an illustrative example, (and for simplicity I didn't check to see if the stream opened correctly).

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

#define ARR_SIZE 31

typedef struct {
    char name[30];
    char address[50];
    double balance;
} ACCOUNT;

int main() {
  ACCOUNT temp, record[ARR_SIZE];
  ifstream ifile("list.txt");
  int i=0;
  double d=0;

  while(i < ARR_SIZE) {
    ifile.getline(temp.name, 30, '\n');//use the size of the array
    ifile.getline(temp.address, 50, '\n');//same here

    //consume the newline still in the stream:    
    if((ifile >> d).get()) { temp.balance = d; }

    record[i] = temp;
    i++;
  }

  for (int i=0; i < ARR_SIZE; i++) {
    cout << record[i].name << "\n" 
         << record[i].address << "\n" 
         << record[i].balance << "\n\n";
  }
  return 0;
}

Another recommendation would be to use vectors for record array, and strings instead of char arrays.

REFERENCES:

Why does std::getline() skip input after a formatted extraction?

Upvotes: 1

Related Questions