Trouble reading a data file twice. (ifstream, c++)

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
I just tried to do the same thing without using .eof and I have the same result, so maybe my logic is really off...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
while(1)
{                cout << "Enter L.";
                 cin >> directory;
                 switch(directory){
                 
                 case 'L':
                 fin.seekg(0);
                 fin >> n >> agentinfo[n].surname >> agentinfo[n].name >> agentinfo[n].code;
                 while(n!=x)
                 {
                 x=n;
                 cout << n << " " << agentinfo[n].surname << " " << agentinfo[n].name << " " << agentinfo[n].code<< "\n";
                 fin >> n >> agentinfo[n].surname >> agentinfo[n].name >> agentinfo[n].code;
                 }
                 cin.ignore();
                 break;


It works once, then the next time it returns nothing. I don't understand.
.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()
I tried that, and the second thing I posted, but I am still having the issue.
Once eof is set, you need to clear it with fin.clear(); before you can use fin again.
I LOVE YOU.
Topic archived. No new replies allowed.