0

4;
Spadina;76 156
Bathurst;121 291
Keele;70 61
Bay;158 158

This is what file contains in it. I need to read them and save them into variables.

4 is for dynamic memory allocation. 4 means there are 4 stations. Spadina, Bathrust, etc.. they are station names. first number, which comes right after station names, is number of student passes and the second number is number of adult pass.

So, basically I have 4 variables and they are;

int numberOfStation;
int studentPass;
int adultPass;
string stationName;

I spent 4 hours but still cannot read the file and save it into variable
Thank you.

cyonder
  • 852
  • 1
  • 15
  • 36
  • 3
    Can you post the code you have by now? – VAndrei Feb 12 '15 at 20:26
  • 3
    Have you looked online, in a book, etc. for information about [C++ I/O](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=c%2b%2b%20i%2fo)? – crashmstr Feb 12 '15 at 20:31
  • Please, please, please, search StackOverflow for "c++ read from file" to get a plethora of examples on how to input data from a text file. [Examples of reading data files](https://www.google.com/search?q=stackoverflow+c%2B%2B+read+from+file&ie=utf-8&oe=utf-8) – Thomas Matthews Feb 12 '15 at 23:29

3 Answers3

4

A possible solution is to read every line with e.g. std::getline then parse each such line string. You'll use the appropriate methods of std::string to search inside it (with find) and split it (with substr). You might also access some individual character in that string using at or the [] operator of std::string; alternatively, you might perhaps parse each line -or relevant parts of them- using std::istringstream (I am not sure it is appropriate in your case). You might be interested by std::to_string...

An important thing is to define exactly (not only thru examples) the possible acceptable inputs. You could for example use some EBNF to formalize that. You should probably care about character encoding (try first by assuming a simple single-byte encoding like ASCII, then later consider UTF-8 if your system uses it).

For example, can the station names (I guess you talk about subway stations) contain digits, or spaces, or underscores, or commas, etc.... ? Could they be French names like Hôtel de Ville (a metro station in central Paris) or Saint-Paul or Bourg-La-Reine (where I am), or Russian names like Молодёжная in Moscow? (I guess that station names in Tokyo or in Jerusalem might be even funnier to parse).

BTW, explicitly entering the number of entries (like your initial 4) is very user-unfriendly. You could have some lexical conventions and e.g. use some tagging or separators.

At last, you might want to keep the information for every travel. Then you'll probably need to define some struct or class (not simply four scalar variables). At that point your program is becoming more interesting!

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

I recommend that you create a struct to hold your stations:

struct station{
    string _stationName;
    int _studentPass;
    int _adultPass;
};

Then create an operator to use with your struct (props to Sly_TheKing for the getline idea):

std::istream& operator>>(std::istream& is, station& rhs){
    getline(is, rhs._stationName, ';');
    is >> rhs._studentPass >> rhs._adultPass >> ws;
    return is;
}

Say that your ifstream is called foo. You can read these into a vector like this:

foo.ignore(numeric_limits<streamsize>::max(), '\n');

vector<station> bar{istream_iterator<station>(foo), istream_iterator<station>()};
Community
  • 1
  • 1
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
1

First group your variables in struct.

struct MyStruct
    {
        int studentPass;
        int adultPass;
        string stationName;
    };

Now read size of struct in file and allocate it dynamically

MyStruct *p;

s >> N;
p = new MyStruct[N];

Now in for loop you read string with delimiter ';' and other two vars are ints

for (int i = 0; i < N; i++)
{
    getline(s, p[i].stationName, ';');
    s >> p[i].studentPass >> p[i].adultPass;
}

Where var s is istream type of variable with flag std::in

Sly_TheKing
  • 518
  • 7
  • 14