creating 3d with mesh, problem with face

Hello
I want to create a torus
I have found the points and it is easy to write to a file
the code works fine when we have 4 faces around for the polygon

But I don’t know how to write the code so that it could be accomplished with any polygon with any number of faces

the write mesh code is as follows

void Mesh::writemesh(char *out_path){
	fstream outfile;
	double p=3.14;double r=3.0;
	outfile.open(out_path,ios::out);
	outfile<<96<<" "<<20<<" "<<134<<"

";
	double N=4.0;
	GLfloat theta = ( 2.0f * 3.1415926536 ) / N;
	for(double i=0;i<=10;i+=(p/6))
	{
		r+=(p/6);
		for(double c=0;c<N;c++){
		outfile<<r*cos(theta * c)<<" "<<r*sin(theta * c)<<" "<<i<<"
";
		}


		/*outfile<<(4+cos(r*p/6))*cos(i)<<" "<<(4+cos(r*p/6))*cos(i)+1.0<<" "<<(r*p/6)*sin(i)+2.0<<"
";
		outfile<<(4+cos(r*p/6))*cos(i)<<" "<<(4+cos(r*p/6))*cos(i)+2.0<<" "<<(r*p/6)*sin(i)+2.0<<"
";
		outfile<<(4+cos(r*p/6))*cos(i)<<" "<<(4+cos(r*p/6))*cos(i)+2.0<<" "<<(r*p/6)*sin(i)+1.0<<"
";
		outfile<<(4+cos(r*p/6))*cos(i)<<" "<<(4+cos(r*p/6))*cos(i)+1.0<<" "<<(r*p/6)*sin(i)+1.0<<"
";*/
	}
	outfile<<"
";
	outfile<<"0 0 0"<<"
";
	outfile<<"
";
	for(double i=0;i<48;i=i+4.0)
	{
		outfile<<N;
		for(double j=0;j<N;j++)
		{
			outfile<<" "<<i+j<<" 0";
		}
		outfile<<"
";

		outfile<<"4 "<<i+1<<" 0 "<<i+5.0<<" 0 "<<i+6.0<<" 0 "<<i+2.0<<" 0"<<"
";
		outfile<<"4 "<<i<<" 0 "<<i+4.0<<" 0 "<<i+5.0<<" 0 "<<i+1.0<<" 0"<<"
";
		outfile<<"4 "<<i+2.0<<" 0 "<<i+6.0<<" 0 "<<i+7.0<<" 0 "<<i+3.0<<" 0"<<"
";
		outfile<<"4 "<<i<<" 0 "<<i+3.0<<" 0 "<<i+7<<" 0 "<<i+4.0<<" 0"<<"
";	
	}
	outfile.close();
}