0

I am trying to read lines of a file (cityName, hiTemp, loTemp) into a struct array. I was able to use >> to read the first several lines until I hit a city with a space in it's name.

I then tried using getline() to read the lines, but then my while loop stopped working.

I have no clue why this would happen.

int LoadData()
{
int count = 0;
string path;


cout << "Specify the input file path: ";
ifstream inFile;
cin >> path;

inFile.open(path.c_str());

if (!inFile.is_open())
{
    cout << "Error - could not open file: " << path;

    return (-1);
}

else
{
    while (!inFile.eof())
    {

        cities[count].city = "";
        getline(inFile, cities[count].city);

        if (cities[count].city.length() == 0)
        {
            break;
        }

        char comma;
        inFile >> (cities[count].high) >> comma >> cities[count].low;
        cout << cities[count].city << " " << cities[count].high << " " << cities[count].low << endl;

        count++;

    }

    inFile.close();
    inFile.clear(std::ios_base::goodbit);
    return count;
}


}
cbuchart
  • 10,847
  • 9
  • 53
  • 93

2 Answers2

2

Use getline as loop condition. You can also replace the second read with a getline too and use a stringstream to parse it.

#include <sstream>
// ...
while(getline(inFile, cities[count].city)) {
  if (cities[count].city.empty()) break;
  // read next line with high and low values
  string str;
  if (!getline(inFile, str)) break; // error in file format
  stringstream ss(str);
  char comma;
  ss >> cities[count].high >> comma >> cities[count].low; // parse it
}
cbuchart
  • 10,847
  • 9
  • 53
  • 93
2
while (!inFile.eof())

For getting every line in the file, you should use:

while(getline(inFile, cities[count].city)) {
    // ...

This works and is recommended over using the .eof() method.

You can also use this in your if-statement:

if (!getline(inFile, str)) 
    break;

As an aside, you can read this site:

Why is “while ( !feof (file) )” always wrong? - StackOverflow post

It gives insight into why using the .eof() is not the preferred method to use in a while loop to check whether the end-of-file has been reached.

Community
  • 1
  • 1
BusyProgrammer
  • 2,783
  • 5
  • 18
  • 31
  • @DanielFeasler Did you take out the getline() from inside the while() loop? Since you have it in your conditon, you do not need it inside your while loop. – BusyProgrammer Mar 19 '17 at 23:52
  • Again, I cannot read your code from the comments section. Please update your question post, @DanielFeasler. – BusyProgrammer Mar 19 '17 at 23:57
  • `inFile >> (cities[count].high) >> comma >> cities[count].low;` why do you have this line? Kf you are reading the line – BusyProgrammer Mar 20 '17 at 00:00
  • The file I am trying to read is a list of Cities with a high and low temp. like this : `Katmandu, -34, 28` – Daniel Feasler Mar 20 '17 at 00:03
  • @DanielFeasler The getline() reads a whole line from a file. So if you have "_Katmandu, -34, 28_" in one line of that file, then the `cities[count].city` should save that whole line from that file. – BusyProgrammer Mar 20 '17 at 00:59
  • I understand that, my question is why is the while loop not looping through? When I use ">>" it works until I get to a city with a two word name. When I use getline, it only runs the first line. – Daniel Feasler Mar 20 '17 at 01:15
  • @DanielFeasler The ">>" is used if there is no space between 2 words; this operator does not continue reading a file, or waiting for input like in "cin<<". On the other hand, getline() only reads the line until you hit the ENTER key, that is why it will not go on to read the next line, as a newline character seperates the 2 of them. – BusyProgrammer Mar 20 '17 at 01:18