stuck in infinite loop

I'm not sure why an infinite loop occurs it should eventually terminate,personStart is equal to 447 I checked and confirmed this while the loop was running and it just kept printing it out infinitely

and the file pointer in.tellg() is at 7

I converted it to an int and 7 is less than 447 yes but the getline functions should move the file pointer eventually ending the loop but it doesn't appear to be doing this

the first index numbers get printed out fine than -1 keeps on getting printed out



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
  vector<Person> people;
   map<int,Index> index;
   stringstream ss;

   ifstream in;
   in.open("randomAccess.txt");



   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();
       cout << "file p" << in.tellg() << endl;
       while((int)(in.tellg()) < personStart){

          cout << in.tellg() <<  " : " << personStart << endl;


       }
   }
any body have any idea?

this problem is driving me crazy
1
2
3
4
5
6
    while((int)(in.tellg()) < personStart){

          cout << in.tellg() <<  " : " << personStart << endl;


       }


Why would this loop not run forever? in.tellg doesn't change inside the loop, so why would it ever end?
Last edited on
very true good point Repeater but

I tried changing the file pointer inside the second and it still got stuck in an infinite loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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();
       cout << "file p" << in.tellg() << endl;
       while((int)(in.tellg()) < personStart){

          cout << in.tellg() <<  " : " << personStart << endl;
          string indexNumber;
         getline(cin,indexNumber,':');

       }
   }


still gets stuck in an infinite loop even though in.tellg() is changing with every read
Last edited on
I didn't think reading from a file randomly would be so much work,

first of all I don't know how the math worked I just counted how many spaces the index size was off by and subtracted it,if anybody can tell me where I went wrong calculating the index size that would be great

here is the code as said I didn't think it would get that complicated spent hours on this

note the commented out code is the code used to write to the file

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285

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

using namespace std;

class Index{

  public:

  int locationID;
  int startByte;
  int size;

  Index(){ }
   Index(int s,int siz){

     startByte = s;
     size = siz;

   }

    Index(const Index& other){

       startByte = other.startByte;
       locationID = other.locationID;
       size = other.size;
    }

    Index& operator=(const Index& other){

       startByte = other.startByte;
       locationID = other.locationID;
       size = other.size;

       return *this;
    }
};

class Person{

  public:

      string name;
      int age;

    Person(){}

   Person(string n,int a){

     name = n;
     age = a;
   }

    Person(const Person& other){

        name = other.name;
        age = other.age;
    }
    Person& operator=(const Person& other){

        name = other.name;
        age = other.age;
        return *this;
    }
};

void loadPerson(ifstream &in,map<int,Index> &index,stringstream &ss){

    // load a person dynamically from a txt file using indexes

     cout << "enter index you want" << endl;
     int inde;
     cin >> inde;
     Index *ind;

     for(map<int,Index>::iterator it = index.begin(); it != index.end(); it++){

        if(it->second.locationID == inde){

            ind = new Index(it->second.startByte,it->second.size);
            cout << "start by " << ind->startByte << endl;
            in.seekg(ind->startByte);
            cout << "file pointer is at : " << in.tellg() << endl;;

            string name;
            getline(in,name,':');

            string age;
            getline(in,age,':');

            cout << "age : " << age << "name :" << name << endl;

            int ageNum;
            ss << age;
            ss >> ageNum;
            ss.str(string()); // clear string stream
            ss.clear();
        }
     }
}

void loadPople(ifstream &in,map<int,Index> &index,stringstream &ss,vector<Person> &people){


        for(map<int,Index>::iterator it = index.begin(); it != index.end(); it++){

            in.seekg(it->second.startByte);

            string name;
            getline(in,name,':');

            string age;
            getline(in,age,':');

            cout << "age : " << age << "name :" << name << endl;

            int ageNum;
            ss << age;
            ss >> ageNum;
            ss.str(string()); // clear string stream
            ss.clear();

            Person p(name,ageNum);

            people.push_back(p);
        }
}

int main()
{
   //ofstream out;
  // out.open("randomAccess.txt");
       vector<Person> people;
       map<int,Index> index;
       stringstream ss;

       ifstream in;
       in.open("randomAccess.txt");

       int hello = 6;
       string size;
       getline(in,size,':');
       if(in.fail()){
          cout << "fail" << endl;
       }
       string personStartString;
       getline(in,personStartString,':');
       if(in.fail()){
           cout << "fail" << endl;
       }
       int personStart = 0;

       ss << personStartString;
       ss >> personStart;

       cout << "person start : " << personStart<< endl;
       ss.str(string()); // clear string stream
       ss.clear();
       int i = 0;

       while(in.tellg() < personStart){

           string indexString;
           getline(in,indexString,':');
           int indexNumber;
           ss << indexString;
           ss >> indexNumber;
           cout << in.tellg() << endl;
           ss.str(string()); // clear string stream
           ss.clear();

           string startByteString;
           getline(in,startByteString,':');
           int startByteNumber;
           ss << startByteString;
           ss >> startByteNumber;
           cout << in.tellg() << endl;
           ss.str(string()); // clear string stream
           ss.clear();

           string sizeString;
           getline(in,sizeString,':');
           int sizeNumber;
           ss << sizeString;
           ss >> sizeNumber;
           cout << in.tellg() << endl;
           ss.str(string()); // clear string stream
           ss.clear();

           Index ind(startByteNumber,sizeNumber);
           ind.locationID = i;
           index[indexNumber] = ind;

           cout << "i: " << i << endl;
           i++;
       }

   for(map<int,Index>::iterator it = index.begin(); it != index.end(); it++){

       cout << "index number : " << it->second.locationID << endl;
       cout << "start byte : " << it->second.startByte << endl;
       cout <<  "size : " << it->second.size << endl;

   }

   loadPerson(in,index,ss);
   loadPople(in,index,ss,people);

   for(int i = 0; i < people.size(); i++){ // print vector of people loaded from file

       cout << "name : " << people[i].name << endl;
       cout << "age : " << people[i].age << endl;
   }
}

//   for(int i = 0; i < 50; i++){
//
//       Person temp("adam",i);
//       people.push_back(temp);
//   }
//    try{
//
//     int size = people.size();
//     out << size;
//     out << ":";
//
//     cout << out.tellp() << endl;
//
//     //int indexSize((9 * 1 * 3) + (41 * 2 * 3) + 16 + 150 - 5);
//     int indexSize = (10 * 1) + (40 * 2) + 50 + (3 * 50) + 50 +(3 * 50) + 50 + (50*1) + 50-199;
//
//     int personStart = (int)(out.tellp() + indexSize + 3);
//
//     out << personStart;
//     out << ":";
//
//     int indexStart = (int)out.tellp();
//
//     out << indexStart;
//     out << ":";
//
//     int startPointer = personStart;
//     out.seekp(personStart);
//
//     startPointer = (int) out.tellp();
//
//     for(int i = 0; i < people.size(); i++){
//
//        out << people[i].name;
//        out << ":";
//        out << people[i].age;
//        out << ":";
//        // +100
//
//        cout << "sp" << startPointer << endl;
//        cout << "tellp" << out.tellp() << endl;
//
//        Index ind(startPointer,(int)(out.tellp() - startPointer));
//        ind.locationID = i;
//        index[i] = ind;
//        startPointer = (int) (out.tellp());
//     }  // 50 + 234 = 284
//
//     out.seekp(indexStart);
//
//     for(map<int,Index>::iterator it = index.begin(); it != index.end(); it++ ){
//
//        int number = it->first;
//        out << number;
//        out << ":";
//        out << it->second.startByte;
//        out << ":";
//        out << it->second.size;
//        cout << it->second.size << endl;;
//        out << ":";
//        //150
//     }
//    }catch(int e){
//
//       cout << e << endl;
//    } 
Last edited on
How do you know it's changing? Are you dumping the value of in.tellg() to console every time round the loop?

hi Repeater not in the code I showed but yes I did try to print the value of in.tellg() in the nested while loop and it displayed correctly for a while print out numbers you would expect than it would constantly print -1 and -1 would get print infinitely
So in fact it's NOT changing. It's -1 all the time.

Now we look up the documentation: http://en.cppreference.com/w/cpp/io/basic_istream/tellg

-1 indicates a failure. Something has gone wrong. Perhaps the end of the file has been reached. Perhaps there was an error reading from the file. Functions exist, such as fail() , that you can use to check what he problem is, but your code needs to handle failures and not just assume that everything always works.
very nice spot Repeater thats exactly the problem =)

I was almost pulling my hair out wondering where the -1 was coming from

thanks
Topic archived. No new replies allowed.