FreeFem-sources icon indicating copy to clipboard operation
FreeFem-sources copied to clipboard

File status query not working

Open pazathoth opened this issue 5 years ago • 0 comments

I am trying to read from a file of unknown length but neither .good() nor .eof seem to indicate the end of the file – I check them both before and after the single input statement but I still get a runtime error that is not even caught by a try ... catch block. A simple self-contained script that reproduces the error is given below.

{
  ofstream simple("simple.dat");
  simple << 1.0 << "\n" << 2.0 << "\n" << 3.0 << endl;
}
{
  ifstream simple("simple.dat");
  int k = 1;
  real val;
  while (1) {
    cout << "value on line " << k++ << ": ";
    if (!simple.good() || simple.eof) break;
    try {
      simple >> val;
    } catch (...) {
      break;
    }
    if (!simple.good() || simple.eof) break;
    cout << val << endl;
  }
}

This is the output by FreeFem++ of the above script:

-- FreeFem++ v4.400003 (2020-05-13T17:30:25 CEST - git no git)
 Load: lg_fem lg_mesh lg_mesh3 eigenvalue 
    1 : {
    2 :   ofstream simple("simple.dat");
    3 :   simple << 1.0 << "\n" << 2.0 << "\n" << 3.0 << endl;
    4 : }
    5 : {
    6 :   ifstream simple("simple.dat");
    7 :   int k = 1;
    8 :   real val;
    9 :   while (1) {
   10 :     cout << "value on line " << k++ << ": ";
   11 :     if (!simple.good() || simple.eof) break;
   12 :     try {
   13 :       simple >> val;
   14 :     } catch (...) {
   15 :       break;
   16 :     }
   17 :     if (!simple.good() || simple.eof) break;
   18 :     cout << val << endl;
   19 :   }
   20 : }
   21 :  sizestack + 1024 =1640  ( 616 )

value on line 1: 1
value on line 2: 2
value on line 3: 3
value on line 4:   current line = 13
Exec error : Fatal Error: file  not good in read array (Op_ReadKN)
   -- number :1
times: compile 0.004294s, execution 0.000378s,  mpirank:0
 CodeAlloc : nb ptr  3352,  size :449464 mpirank: 0
Ok: Normal End

The documentation is not very forthcoming on any of these functions/statements and they are apparently not working as would be expected from C++.

pazathoth avatar May 29 '20 16:05 pazathoth