Reading from a text file to class objects

Pages: 12
Hello all,

I have a question regarding my code. I would like to obtain information from a text file and assign that information into class objects. I will use the information from objects later on to write a program.

My text file looks like this:
984763 A 20 18
762613 A 19 17
587123 A 22 16
897654 D 85 19

where the first columns represent ID#, Type, processing time 1, processing time 2.

This is my code:

[

#include <iostream>
#include <string>
#include <fstream>


using namespace std;

class trucks {
private:
int ID;
string Type;
double feeder_pt;
double axle_pt;

public:
void getID (int);
void getType (string);
void getfeeder (double);
void getaxle (double);

};

void trucks::getID(int ID)
{
}

void trucks::getType (string Type)
{
}

void trucks::getfeeder (double feeder_pt)
{
}

void trucks::getaxle (double axle_pt)
{

}

int main () {
ifstream myfile("sequence.txt");
int i=0;
int ID;
string Type;
double feeder_pt;
double axle_pt;

// the size left as 5 for testing purposes only
for(int i=0; i<5; i++)
{

trucks truck[i];
truck[i].getID(ID);
truck[i].getType(Type);
truck[i].getfeeder(feeder_pt);
truck[i].getaxle(axle_pt);
myfile>>ID>>feeder_pt>>axle_pt;

}

// to test if the code works

cout<< ID << endl;
cout<< axle_pt << endl;
cout<< Type << endl;
cout<< axle_pt << endl;
} ]

The output gives me the first element in the text file (984763) and nothing for the rest. It seems that the file is opening but there is something wrong with my code. Any help please?

Thank you,
Last edited on
One way to read from a stream:
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
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>

using namespace std;

int main ()
{
  ifstream myfile ("sequence.txt");
  if (!myfile)
  {
    perror ("File error: ");
    system ("pause");
    exit (EXIT_FAILURE);
  }
  const int NUM_TRUCKS = 5;
  int id = 0, time1 = 0, time2 = 0, i = 0;
  string type;
  while (myfile >> id >> type >> time1 >> time2 && i++ < NUM_TRUCKS)
  {
    // use the input
    cout << id << '\t' << type << '\t' << time1 << '\t' << time2 << '\n';
  }

  system ("pause");
  return 0;
}
Hi,
1
2
3
4
5
6
7
8
9
10
11
12
13
for(int i=0; i<5; i++)
{
trucks truck[i];
truck[i].getID(ID);
truck[i].getType(Type);
truck[i].getfeeder(feeder_pt);
truck[i].getaxle(axle_pt);
myfile>>ID>>feeder_pt>>axle_pt;
}
cout<< ID << endl;
cout<< axle_pt << endl;
cout<< Type << endl;
cout<< axle_pt << endl;



Should be :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
if (!myfile)
 {
    perror ("File error: ");
    system ("pause");
    exit (EXIT_FAILURE);
 }

trucks truck[5];
for(int i = 0; i < 5; i++)
if(myfile >> ID >> Type >> feeder_pt >> axle_pt)
 {
     truck[i].ID = ID;
     truck[i].Type = Type;
     truck[i].feeder_pt = feeder_pt;
     truck[i].axle_pt = axle_pt;
     cout << ID << endl;
     cout << Type << endl;
     cout << feeder_pt << endl;
     cout << axle_pt << endl;
 }
Last edited on
Does that help you? :)
Thank you Thomas1965. I guess what I am trying to do is not to just read the text file, I would also want to store it into class objects. This is where I face a difficulty the most.

To get the data into your object you need to implement some setters in you truck class.
For example:
1
2
3
4
truck::setID(int ID)
{
   this->ID = ID;
}

> I would also want to store it into class objects.
I edited my solution. Follow my example.
Thank you 5a8Ym39o6. I receive an error when I write your code. I guess the problem with the code you provided is that I have to declare the array size while it should be declared based on the text file each time the code is run. Also, I have the following variable in private:
ID, Type, feeder_pt, axle_pt which the program cannot access on main.



Thank you so much Thomas1965. Do I do that in public or private? Also, do I need to write void before truck? Also, do I keep the rest of the code the same?

Sorry I know it might be a stupid question that I am asking but I am just a beginner.

> Also, I have the following variable in private:
ID, Type, feeder_pt, axle_pt
Then just have these variables become public and see the result.
Thank you so much 5a8Ym39o6 for your edit. I just get a lot of errors when I run the code because it seems that all the variables are declared in private which main could not access. Also, where do I define the array trucks [5]? or is it the class that I already defined?
> Also, where do I define the array trucks [5]? or is it the class that I already defined?
It is in the place of where it is (in my code). No need to change its original place.
Thank you 5a8Ym39o6 but the code still has a lot of errors even after declaring the variables in public. It says that the variables (i.e ID, Type, axle_pt, feeder_pt) are not declared in this scope. Also, the IF statements in the code give me errors so I run the program without the if statements.
Well, try this :
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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class trucks {
public :
    int ID;
    string Type;
    double feeder_pt;
    double axle_pt;
public:
    void setID (int id) {ID = id;}
    void setType (string type) {Type = type;}
    void setfeeder (double feeder) {feeder_pt = feeder;}
    void setaxle (double axle) {axle_pt = axle;}
};

int main ()
{
    ifstream myfile("sequence.txt");
    int ID;
    string Type;
    double feeder_pt;
    double axle_pt;
    if (!myfile)
    {
        cout << ("Error: sequence.txt not found");
        system ("pause");
        exit (EXIT_FAILURE);
     }

    trucks truck[5];
    for(int i = 0; i < 5; i++)
    if(myfile >> ID >> Type >> feeder_pt >> axle_pt)
     {
         truck[i].setID(ID);
         truck[i].setType(Type);
         truck[i].setfeeder(feeder_pt);
         truck[i].setaxle(axle_pt);
         cout << truck[i].ID << " ";
         cout << truck[i].Type << " ";
         cout << truck[i].feeder_pt << " ";
         cout << truck[i].axle_pt << endl;
     }
}
Last edited on
(Does that help you?) :)
It helps me SO MUCH 5a8Ym39o6 . Thank you.
I runned it without these line because I receive an error.

if (!myfile)
{
cout << ("Error: sequence.txt not found");
system ("pause");
exit (EXIT_FAILURE);
}

Is there is a way that I can run the for loop until the end of the text file, without defining 5 as the termination condition?

Thank you again :)
> Is there is a way that I can run the for loop until the end of the text file, without defining 5 as the termination condition?

Then this :
1
2
3
trucks truck[5];
    for(int i = 0; i < 5; i++)
    if(myfile >> ID >> Type >> feeder_pt >> axle_pt)


You change it to :
1
2
trucks truck[800];
    while(myfile >> ID >> Type >> feeder_pt >> axle_pt)
Does that help? :)
No that really doesn't help, you now have the possibility of accessing the array out of bounds if your file contains more than 800 records. The real answer would be to use std::vector instead of the array and push the records into the vector as you go letting the vector resize as necessary to hold the contents of the file.

Does that help? :)


Posting this after every reply you send is extremely annoying, 5a8Ym39o6
Pages: 12