Help with reading a file

So basically, I got a file with the following content in it:
4 //Number of stations
Spadina;76 156 //Name, number of student passes, and number of adult passes
Bathurst;121 291
Keele;70 61
Bay;158 158

i got an object that takes 3 paramaters, the name of the station for example spadina, the number of studnet passes so 76, and number of adult passes so 156. how can i read the file and set it ?
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
std::vector<Station> stations;
if(std::ifstream in {"stations.txt"}) //open the file
{
    std::size_t number_of_stations;
    if(in >> number_of_stations) //read the number of stations
    {
        std::string name;
        std::size_t students, adults;
        for(std::size_t i = 0; //split onto multiple lines for readability
            (i < number_of_stations) && //make sure we don't read too much
            std::getline(in, name, ';') && //read the name
            (in >> students >> adults) && //read the number of passes
            in.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //ignore the rest of the line
            ++i)
        {
            stations.push_back(Station(name, students, adults)); //add the station
        }
    }
    else
    {
        std::cerr << "Couldn't read number of stations" << std::cerr;
    }
}
else
{
    std::cerr << "Couldn't open stations.txt" << std::endl;
}
so it automatically skips the '4' on the first line ?
Oops, nope it doesn't skip the newline character after the '4', I forgot to do that. You can easily fix that by moving line 13 to before line 11.

EDIT: The '4' itself is read in on line 5, but the newline character after it isn't because I forgot.
Last edited on
Hi jattDaBoi,

You can run the following code directly in your code::blocks.. Hope this will help you.

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
#include <iostream>
#include <fstream>
#include <vector>
#include <stdlib.h>

using namespace std;
class Station
{
public:
    Station() {} ;
    Station(std::string stationName,int noOfStudentPasses,int noOfAdultPasses)
    {
        m_stationName = stationName;
        m_noOfStudentPasses = noOfStudentPasses;
        m_noOfAdultPasses = noOfAdultPasses;
    }
    ~Station()
    {

    }
    void printData()
    {
        std::cout<<std::endl;
        std::cout<<"Station Name: "<<m_stationName;
        std::cout<<std::endl<<"No of Student Passes: "<<m_noOfStudentPasses;
        std::cout<<std::endl<<"No of Adult Passes: "<<m_noOfAdultPasses;
        std::cout<<std::endl;
    }
private:
    std::string m_stationName;
    int m_noOfStudentPasses,m_noOfAdultPasses;

};
int main()
{
    std::vector<Station> stationsData;
    std::ifstream ifile("stationData.txt");
    char line[80];
    while (ifile.getline(line,80))
    {
        std::string str = line;

        // If line is empty, skip the line. 
        if(str.empty())
        {
            continue;
        }


        //If line doesn't contain a ';', skip the line. I've used this to
        //skip the first line which contains the number of stations.
        size_t foundSemiColon = str.find(";");
        if(std::string::npos == foundSemiColon)
        {
            continue;
        }
       //Eg. line is Spadina;76 156   -> str = "Spadina;76 156"

        std::string stationName = str.substr(0,str.find_first_of("; "));
        str = str.substr(str.find(';')+1);

        //Now, str = "76 156"

        int noOfStudentPasses = atoi(str.substr(0,str.find_first_of(" \t")).c_str());
        str = str.substr(str.find_first_of(" \t")+1);

        //Now, str = "156"

        int noOfAdultPasses = atoi(str.substr(0,str.find_first_of(" \t\n")).c_str());

        stationsData.push_back(Station(stationName,noOfStudentPasses,noOfAdultPasses));
    }


    //Printing just to make sure we've done everything right.
    std::vector<Station>::iterator it;
    for(it = stationsData.begin();it!=stationsData.end();++it)
    {
        it->printData();
    }

    return 0;
}
Last edited on
You can also make use of static integer data member and increase it as you go on pushing (creating) objects in vector. This would help you determine how many objects you've created.
@HabibAurangabad: there are multiple issues with your code:
- using namespace std; is bad practice (Google will tell you why)
- You neglect to check if the file could be opened successfully
- You hard-code your program to break if a line is linger than 80 characters
- You do all sorts of unnecessary substring math
- You separate declaration from initialization on lines 76 and 77

It is good that you are trying to be helpful, but please be aware of the mistakes you are making. Often the person you are trying to help does not know any better and will learn your mistakes as the right way to do things.
Thank you brother..
I know all the issues which you indicated above...

But yes, you are right. He may learn my mistakes from whatever I've posted.

I'll be careful next time. :)
Topic archived. No new replies allowed.