I have a program due Friday and really need some help. I'm trying to read a file of 500 customer records. Each record consists of a 6-digit customer ID, zip code, 8-digit sales volume, and name. Each field is separated by 5 spaces, with each record on a separate line. When I try to get a total of the records that were read, I get 814, but there are only 500 in the file. Any advice would be greatly appreciated!
Code:
#include <iostream.h>
#include <iomanip.h>
#include <fstream.h>
#include <stdlib.h>
struct custRec {
int IDnum;
int zipCode;
int totalSalesVol;
char name[20];
};
////////////////////////////////////////////////////////////////////////////////
void readFile(custRec c[], int arraySize){
int i=0;
custRec record;
ifstream inCustInfo("random.dat", ios::in);
if (!inCustInfo){
cerr << "File could not be opened." << endl;
exit(1);
};
while (!inCustInfo.eof()){
inCustInfo.read((char *)&record, sizeof(custRec));
c[i]=record;
i++;
};
cout << "There are " << i << " records in the file.";
}
///////////////////////////////////////////////////////////////////////////////
main(){
const int MAXELEMENTS=1000;
custRec customer[MAXELEMENTS];
readFile(customer, MAXELEMENTS);
return 0;
}