Solution Store for dG set up

For discontinuous space set up how to store the solution with x and y values in a text file?
I tried

{
ofstream ff(“phi.txt”);
ff << phi ;
}
savemesh(Th,“Th.msh”);

and

{
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;
ff << Th[i][0].x << " " << Th[i][0].y << " " << phi[Vh(i,0)] << “\n\n\n”
}
}

But it did not work.

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;
}
}
}

Do I need to change anything for P2dc fespace Vh(Th, P2dc)?

for fespace Vh(Th,P2dc) you can do as

{
ofstream ff("graph.txt");
real[int] mix(3),miy(3);
for (int i = 0; i < Th.nt; i++)
{
mix(0)=(Th[i][1].x+Th[i][2].x)/2.;//coordinates of the middle of the edges
miy(0)=(Th[i][1].y+Th[i][2].y)/2.;
mix(1)=(Th[i][0].x+Th[i][2].x)/2.;
miy(1)=(Th[i][0].y+Th[i][2].y)/2.;
mix(2)=(Th[i][0].x+Th[i][1].x)/2.;
miy(2)=(Th[i][0].y+Th[i][1].y)/2.;
for (int j = 0; j < 3; j++){
ff << Th[i][j].x << " "<< Th[i][j].y << " " << phi[][Vh(i,j)] << endl;
ff << mix(j) << " "<< miy(j) << " " << phi[][Vh(i,j+3)] << endl;
}
}
}