Apr 8, 2012 at 11:32pm UTC
Hi, I have been struggling with this aspect of my project for a long time now. I don't think I understand using .eof fully, but maybe this will help.
I made a smaller code just to try to solve my issue. Here it is:
(My problem starts at line 28)
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 39 40 41 42 43 44 45 46 47 48
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <string>
using namespace std;
struct agentlist {
string name, surname;
int code;
double height, weight;
} agentinfo[10];
int n=0;
char directory;
int main()
{
ifstream fin("Testing" );
if (fin.fail())
{
cout << "Data file not found.\n" ;
system("pause" );
exit(0);}
else
while (1)
{ cout << "Enter L." ;
cin >> directory;
switch (directory){
case 'L' :
fin.seekg(0);
while (!fin.eof())
{
fin >> n >> agentinfo[n].surname >> agentinfo[n].name >> agentinfo[n].code;
cout << n << " " << agentinfo[n].surname << " " << agentinfo[n].name << " " << agentinfo[n].code<< "\n" ;
}
cin.ignore();
break ;
default :
cout << "Invalid entry\n" ;
cin.ignore();
break ;
}
I want this code to read my data and display it each time "L" is chosen.
However, it works ONCE and then it displays nothing. I guess that's because I am now at the "end of file" ? I tried setting the get point to the beginning but that didn't work.
Any advice would be really appreciated.
Last edited on Apr 8, 2012 at 11:46pm UTC
Apr 9, 2012 at 12:52am UTC
.eof() is a bool function which determines if the pointer for the file is at the end of the file. it does not work well. try .good()
Apr 9, 2012 at 1:02am UTC
I tried that, and the second thing I posted, but I am still having the issue.
Apr 9, 2012 at 1:13am UTC
Once eof is set, you need to clear it with fin.clear();
before you can use fin again.