border floor(t=0,1){ x=t; y=0; label=1;}; // the unit square
border right(t=0,1){ x=1; y=t; label=5;};
border ceiling(t=1,0){ x=t; y=1; label=5;};
border left(t=1,0){ x=0; y=t; label=5;};
int n=10;
mesh th= buildmesh(floor(n)+right(n)+ceiling(n)+left(n));
savemesh(th,"toto.am_fmt");// format "formated Marrocco"
savemesh(th,"toto.Th");//format database "bamg"
savemesh(th,"toto.dbg");//format debug
savemesh(th,"toto.msh"); //format freefem
mesh th2 = readmesh("toto.msh");
fespace femp1(th,P1);
femp1 f = sin(x)*cos(y),g;
int where;
real xx;
{
  
  ofstream file("f.txt",binary);// for windows add binary version 3.30 
  file.precision(16);
  file << f[] << endl;
  where=file.tellp();
  file << 0.1 ;
  //  file << " " << 0.2 ;
  cout << " where in file " << where << endl;
  file << " # comment bla bla ...  0.3 \n";
  file << 0.2 << endl; 
  file.flush; // to flush the io  buffer of file
}
//  Idea to skip comment in a file ...  start with  # too EOL
func ifstream skipcomment(ifstream &ff)
{
    
    while(1)
    {
    int where = ff.tellg(); // store file position 
    string comment;
    ff >> comment; 
    if ( ! ff.good() ) break; 
    if( comment(0:0)=="#") {
         getline(ff,comment);
         cout << " -- #" << comment << endl;
    }
    else {
        ff.seekg(where); //restore file position 
        break;        
    }    
    }
    return ff;
}


{
  ifstream file("f.txt",binary); // for windows (pb CRNL EOL  translation ) 
  int l = file.length;
  cout << " where " << file.seekg << endl; 
  file.seekg(where);
  file >> xx;
  cout <<  " xx = " << xx << " good ? " << file.good() << endl;
  assert(xx==0.1);
  skipcomment(file) >> xx;
  assert(xx==0.2);
  where =file.tellg();
  assert(!file.eatspace); 
  cout << where << " < " << l << endl; 
  assert(l>=where);
  file.seekg(0);
  cout << " where " << file.tellg() << " " << file.good() << endl; 
  file >> g[] ;
  // nothing after only space
  
}
fespace Vh2(th2,P1);
Vh2 u,v;
plot(g);
solve pb(u,v) =
    int2d(th2)( u*v -dx(u)*dx(v)-dy(u)*dy(v) )
  + int2d(th2)(-g*v) 
  + int1d(th2,5)( -g*v)
  + on(1,u=0) ;
plot (th2,u);
cout << " (u[],u[]) = " << (u[]'*u[]) << endl; 
{
ofstream file("u.txt");
file << u[] << endl;
 file.seekp(100);
 file.flush;
 file  << "xxx "<< endl;
 
}
