Problem with istringstream in a 'while' loop with CINT

Hi,

I have noticed a strange behaviour when running a simple macro that reads a text file with lines of numbers. I just want to read these numbers. The code is below.

{
#include <fstream>
#include <sstream>
{
  using namespace std;

  ifstream f1("list.txt",ios_base::in);
  string line;
  istringstream iss;

  int i = 0;
  float a;

  while(getline(f1,line)){
    iss.str(line);
    cout << line << endl;
    while(iss >> a){
      cout << i << " "  << a << endl;
      i++;
    }
    iss.clear();
    cout<< "end loop " << a << endl;
  }
  f1.close();
}
}

list.txt being for example :
0 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

The second ‘while’ loop stops at the (n-1) element even if the last element in ‘line’ is seen (the “end loop” print out proves that).

And if this small code is compiled, it works well.

I use ROOT v5.26.00a.

What did I miss here ??

Thank you for your help.

Mathieu

Hi,

please don’t use unnamed macros, especially when they #include files. Just give the function you define in the file the same name as the filename (macro() for macro.C) and you can execute it as before.

I get

$ root -l -b t.cxx
root [0] 
Processing t.cxx...
0 1 2 3 4 5 6 7 8 9
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
end loop 9
10 11 12 13 14 15 16 17 18 19
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
end loop 19
20 21 22 23 24 25 26 27 28 29
18 20
19 21
20 22
21 23
22 24
23 25
24 26
25 27
26 28
end loop 29
30 31 32 33 34 35 36 37 38 39 
27 30
28 31
29 32
30 33
31 34
32 35
33 36
34 37
35 38
36 39
end loop 39
root [1] .q

Which I assume is the expected output. That’s with he trunk, so we might have fixed your issue already.

Cheers, Axel.

Hi Axel,

Thank you for your reply. I’ll keep in mind the naming of the functions in macros.

But the problem is still there…

When processing the first line of list.txt (0 1 2 3 4 5 6 7 8 9) , we have:

Processing t.cxx... 
0 1 2 3 4 5 6 7 8 9 
0 0 
1 1 
2 2 
3 3 
4 4 
5 5 
6 6 
7 7 
8 8 
end loop 9 

when I expect this :

Processing t.cxx... 
0 1 2 3 4 5 6 7 8 9 
0 0 
1 1 
2 2 
3 3 
4 4 
5 5 
6 6 
7 7 
8 8 
9 9
end loop 9

Why is the last item not written ? Why the program don’t enter one last time into the while loop ?

I don’t have this problem when I compile (with g++) this little piece of code.

Mathieu

This is fixed by revision 32859 on the root trunk. The problem was that cint was including the state of the eof bit in the if test when the standard says it should use !fail(), which checks only the fail bit and the bad bit, not the eof bit.

Thank you for your report!

– Paul Russo