Reading parts from a text file

This is my coding for reading the text but it doesnot open when it runs...confused !!

if (choice == 1)
{
cout << "Enter filename: " << endl;
cin >> file;
input.open("C:\\myfile.txt",ios::in);
discard_line(input);

while(!input.eof())
{
input >> id >> name >> age >> gender >> country;
ReadFile(ptr,size) //function call
}

total_regs++;
input.get(c);
}

void ReadFile{File *ptr, int &size)
for (int i = 0; i < size; i++)
{
cout << id >> name >> age >> gender >> country;

}
Last edited on
can you post your full code? and by the way, what was the error message?
there was no error message, the cmd just wont display the texts.. after entering the filepath...its just says "Press any key to continue"...
1
2
3
4
5
6
void ReadFile{File *ptr, int &size)
for (int i = 0; i < size; i++)
{
cout << id >> name >> age >> gender >> country;

}

The code above didn't produce an error? Are you sure?
Oh sorry this the code...

void ReadFile{File *ptr, int &size)
for (int i = 0; i < size; i++)
{
cout << (*ptr).id >> (*ptr).name >> (*ptr).age >> (*ptr).gender >> (*ptr).country;

}
Posting the full code or at least giving us a description of exactly what you are trying to do would certainly help.

It looks like you want associate the user's inputted file name with the input object.
If file is an std::string, then you will want to change input.open("C:\\myfile.txt",ios::in); to input.open(file.c_str(), ios::in);.

It also looks like you want to pull a set of values from the input stream (id, name, age, gender, country), then output those values, then repeat for the next set of those values. It is difficult to correct your code though, as we cannot see the whole picture.
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <string>

const int TOTAL_REG = 20; //equal to the number of registrations

using namespace std;

struct Athlete {
int aID;
string aName;
int aAge;
char aGender;
string aCountry;
};

struct Event {
int eID;
string eName;
string eRecord;
};

struct Registration {
int rID;
Athlete rAthlete;
Event rEvent;
};

//function prototypes
void discard_line(ifstream &input);
void ReadRegistration(Registration *regPtr, int &size);

int main()
{
Registration regs[TOTAL_REG];
Registration regPtr[TOTAL_REG];

int size;
int total_regs;
char file[100];
int menu;
ifstream input;
char c;

cout << "Welcome to Athlete Registration and Result Program" << endl;
cout << "\nMenu: \n1 - Read and Display registration file \n";
cout << "2 - List all athletes by event \n";
cout << "3 - List all athletes by country \n";
cout << "4 - Read and display result file \n";
cout << "5 - List medals counts by country \n";
cout << "6 - Display medal tally \n";
cout << "7 - Exit \n\n";
cout << "Please Enter menu option: ";
cin >> menu;

if (menu == 1)
{
cout << "Enter Registration filename: " << endl;
cin >> file;
input.open("C:\\TEMP\\registration.txt",ios::in);
if (input.fail()) {
cerr << "Filename error" << endl;
}
discard_line(input);
total_regs = 0;
while(!input.eof())
{
input >> regs -> rID >> (*regPtr).rAthlete.aID >> (*regPtr).rAthlete.aName >> (*regPtr).rAthlete.aAge >> (*regPtr).rAthlete.aGender >> (*regPtr).rAthlete.aCountry >> (*regPtr).rEvent.eID >> (*regPtr).rEvent.eName >> (*regPtr).rEvent.eRecord;
ReadRegistration(regPtr, size);
}
total_regs++;
input.get(c);
input.close;
}

system("PAUSE");
return 0;
}
//discard_line function can be used to discard any unneeded line from an input file
void discard_line(ifstream &in)
{
char c;

do
in.get(c);
while (c!='\n');
}
//Definition of function to read registration.
void ReadRegistration(Registration *regPtr, int &size)
{
for (int i = 0; i < size; i++)
{
cout << (*regPtr).rID << " " << (*regPtr).rAthlete.aID << " " << (*regPtr).rAthlete.aName << " " << (*regPtr).rAthlete.aAge << " " << (*regPtr).rAthlete.aGender << " " << (*regPtr).rAthlete.aCountry << " " << (*regPtr).rEvent.eID << " " << (*regPtr).rEvent.eName << " " << (*regPtr).rEvent.eRecord;
}

}
and you should using code tag...
Since the code was fairly large, I fixed the biggest issues and added comments within /** **/, I hope it makes sense.

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>

const int TOTAL_REG = 20; //equal to the number of registrations

using namespace std;

struct Athlete {
  int aID;
  string aName;
  int aAge;
  char aGender;
  string aCountry;
};

struct Event {
  int eID;
  string eName;
  string eRecord;
};

struct Registration {
  int rID;
  Athlete rAthlete;
  Event rEvent;
};

//function prototypes
void discard_line(ifstream &input);
void ReadRegistration(Registration *regPtr, int &size);

int main()
{
  Registration regs[TOTAL_REG]; /** Unused? **/
  Registration regPtr[TOTAL_REG];

  int size;
  int total_regs;
  char file[100];
  int menu;
  ifstream input;
  char c; /** Unused **/

  cout << "Welcome to Athlete Registration and Result Program" << endl;
  cout << "\nMenu: \n1 - Read and Display registration file \n";
  cout << "2 - List all athletes by event \n";
  cout << "3 - List all athletes by country \n";
  cout << "4 - Read and display result file \n";
  cout << "5 - List medals counts by country \n";
  cout << "6 - Display medal tally \n";
  cout << "7 - Exit \n\n";
  cout << "Please Enter menu option: ";
  cin >> menu;

  if (menu == 1)
  {
    cout << "Enter Registration filename: " << endl;
    cin >> file;
    input.open(file);
    
    if (input.fail())
    {
      cerr << "Filename error" << endl;
    }
    discard_line(input);
    total_regs = 0;
    
    int i;
    for (i = 0; !input.eof(); i++)
    {
      /** regPtr is an array, so to access individual Registration objects,
          you can use regPtr[index_number].
      **/
      
      input >> regPtr[i].rID >>
          regPtr[i].rAthlete.aID >>
          regPtr[i].rAthlete.aName >>
          regPtr[i].rAthlete.aAge >>
          regPtr[i].rAthlete.aGender >>
          regPtr[i].rAthlete.aCountry >>
          regPtr[i].rEvent.eID >>
          regPtr[i].rEvent.eName >>
          regPtr[i].rEvent.eRecord;
    }
    
    size = i - 1;
    
    ReadRegistration(regPtr, size);
    
    total_regs++;
    input.get(c);
    input.close(); /** Forgot (). **/
  }

  // system("PAUSE"); /** Should be avoided. On my system this didn't work **/
  cin.get(); /** This should work just to see the output. **/
  return 0;
}

//discard_line function can be used to discard any unneeded line from an input file
void discard_line(ifstream &in)
{
  char c;

  do
  in.get(c);
  while (c!='\n');
}

//Definition of function to read registration.
void ReadRegistration(Registration *regPtr, int &size)
{
  for (int i = 0; i < size; i++)
  {
    /** I added a newline at the end to more easily view the output. **/
    
    cout << regPtr[i].rID << " " <<
        regPtr[i].rAthlete.aID << " " <<
        regPtr[i].rAthlete.aName << " " <<
        regPtr[i].rAthlete.aAge << " " <<
        regPtr[i].rAthlete.aGender << " " <<
        regPtr[i].rAthlete.aCountry << " " <<
        regPtr[i].rEvent.eID << " " <<
        regPtr[i].rEvent.eName << " " <<
        regPtr[i].rEvent.eRecord << '\n';
  }
} 
Last edited on
THANK YOU SO MUCH ! IT WORKS FINE :)
Okay now i have to read only parts from the same text file..buh this codes are not reading anything :

else if (menu == 2)
{
cout << "Enter event ID: " << endl;
cin >> EID;

input.open(regs_txt);

if (
{
cerr << "eID error" << endl;
}

discard_line(input);
total_regs = 0;

int i;
for (i = 0; !input.eof(); i++)
{
/** regPtr is also being used as an array, so to access individual Registration objects,
you can use regPtr[index_number].
**/

input >> regPtr[i].rEvent.eID >>
regPtr[i].rAthlete.aID >>
regPtr[i].rAthlete.aName >>
regPtr[i].rAthlete.aCountry;
}

size = i - 1;

ListAthletesByEvent(regPtr, size, eID);

total_regs++;
input.get(c);
input.close();

[function definition]

void ListAthletesByEvent(Registration *regPtr, int size, int eID)
{
for (int i = 0; i < size; i++)
{

cout << regPtr[i].rAthlete.aID << " " <<
regPtr[i].rAthlete.aName << " " <<
regPtr[i].rAthlete.aCountry << '\n';
}

}


HELP !!
Topic archived. No new replies allowed.