Your syntax is correct for P1dc fespace Vh(Th,P1dc).
Your first try stores the coordinates in the basis of the fespace.
It can be used for loading later in FF++.
Your second try solves the location of dof and the value of the function.
You simply miss {} for the content of the j loop:
{
ofstream ff("graph.txt");
for (int i = 0; i < Th.nt; i++)
{
for (int j = 0; j < 3; j++){
ff << Th[i][j].x << " "<< Th[i][j].y << " " << phi[][Vh(i,j)] << endl;
}
}
}