0

Hi guys I seem to be stuck in an infinite loop and can't figure out for the life of me why it is occuring,

I'm reading from a file I tested and made sure that personStart equals 447,also with some debugging I can confirm in.tellg() equals 7

it seems to work first time around but after it prints all the indexes from 8 to 447 it gets stuck in an infinite loop and keeps printing -1 (the file pointers value)

**edit when I remove the second while loop it still works fine and terminates,so it's not a problem with the first while loop( also not a duplicate and no idea why it would be downvoted)

Ok guys I'll edit the question and code so it's complete but I'm just quite annoyed with this question been marked a duplicate as the above question doesn't help me in the slightest so I've created a txt file to be randomly accessed using indexes now I have run into a problem when trying to read from that txt file I get stuck in an infinite loop,I've tested other code in inside a nested while loop and it runs perfect,if someone could just suggest why it may be happening and what is wrong here I will be more than grateful.

#include <iostream>
#include <fstream>
#include <map>
#include <vector>
#include <sstream>

using namespace std;

int main()
{
   vector<Person> people;
   map<int,Index> index;
   stringstream ss;

   ifstream in;
   in.open("randomAccess.txt");
   int hello = 6;

   while(!in.eof()){

       string size;
       getline(in,size,':');
       string personStartString;
       getline(in,personStartString,':');
       int personStart;
       ss << personStartString;
       ss >> personStart;
       ss.str(string()); // clear string stream
       ss.clear();

       while(in.tellg() < personStart){

           string indexString;
           getline(in,indexString,':');
           int indexNumber;
           ss << indexString;
           ss >> indexNumber;
           cout << in.tellg() << endl;
           if ( in.fail() ){

              cout << "failed" << endl;

           }
       }
   }
irishmaniac
  • 81
  • 10
  • when I remove the second while loop it still works fine and terminates,so it's not a problem with the first while loop( also not a duplicate and no idea why it would be downvoted) – irishmaniac Dec 14 '17 at 21:45
  • I can confirm this is not a duplicate run the code yourself with the following input 1:2:3:4:5:6:7 in a .txt file – irishmaniac Dec 14 '17 at 21:48
  • The question should still be closed, because you didn't provide a complete program (there are no headers, no `main`, no `using` for the names you use from `std` ...) which is probably why you got downvoted. Read [this](https://stackoverflow.com/help/how-to-ask) and [this](https://stackoverflow.com/help/mcve) – Jonathan Wakely Dec 14 '17 at 22:06
  • thanks Jonathan,can I repost the question after 90 minutes again,properly with headers etc?,because this is not a duplicate question,I mean its not even close to the question it was marked as a duplicate of.Thanks – irishmaniac Dec 14 '17 at 22:11
  • 1
    You can edit your question and fix the code. And add some results. – drescherjm Dec 14 '17 at 22:14
  • ***I mean its not even close to the question it was marked as a duplicate of*** I am not sure of that. You are improperly using eof() in a while loop. – drescherjm Dec 14 '17 at 22:15
  • Edit the question, don't post a new one. – Jonathan Wakely Dec 14 '17 at 22:16
  • agreed I probably am but that is not the issue because I tried other while loops nested inside that while loop and they all pass,its the condition inside the second while loop that is failing somehow,the other question doesn't begin to help me – irishmaniac Dec 14 '17 at 22:17
  • Please read https://stackoverflow.com/help/mcve again, the code you've posted is not minimal (the `Index` and `Person` classes are completely irrelevant to the problem). – Jonathan Wakely Dec 14 '17 at 22:24
  • sure thing Jonathan I'll remove them – irishmaniac Dec 14 '17 at 22:25

1 Answers1

1

Your code uses the first two of my C++ antipatterns:

Reading from an istream without checking the result

and

Testing for istream.eof() in a loop

The reason it loops forever is the first one: you are reading from the file but not checking if that works. If your input file contains "1:2:3:4:5:6:7" then it has an odd number of fields, so you read a size but not a personStart, and then enter the inner loop and never leave it, because all the I/O operations in that loop fail.

Don't use eof() in streams, it is not the right condition. You should check if your input operations succeed, not if you've reached the end of the file (because if an operation fails, the stream goes into a failed state and never reaches EOF).

Jonathan Wakely
  • 153,269
  • 21
  • 303
  • 482