I've written a program like the follow:

int main ()
{
	.
	.
	.
	.
	

	ifstream file1;
	ifstream file2;
	ofstream simiralities;
	simiralities.open("P(0)_P(1).txt", ios::app);
	file1.open("test1.txt", ios::in);
	
	while (!file1.eof())
	{
		.
		.
		.


		file2.open("test2.txt", ios::in);
		
				
		while (!file2.eof())
		{
			
			.
			.
			.
			.
			
		}

		file2.close();
				
		
	}

	simiralities<<number;
	file1.close();
	simiralities.close();

	return 0;
}

the inner loop just works for the first time. from the scond time in outer loop, I can't inter the inner loop. I gusse there migth be a problem with file.close().

Recommended Answers

All 3 Replies

Member Avatar for jencas

Maybe a file2.clear() would be helpful because upon completion of the first loop the eof bit remains set.

1.
      int main ()
   2.
      {
   3.
      .
   4.
      .
   5.
      .
   6.
      .
   7.
       
   8.
       
   9.
      ifstream file1;
  10.
      ifstream file2;
  11.
      ofstream simiralities;
  12.
      simiralities.open("P(0)_P(1).txt", ios::app);
  13.
      file1.open("test1.txt", ios::in);
  14.
       // A good way to check if a file has been opened-

      if (! file1)

      {
       cout << "error opening file" << endl;
       return -1;
      /*Your program isn't opening your files correctly, and will exit here. Have a gander & see what you can come up with*/
      }
  15.
      while (!file1.eof())
  16.
      {
  17.
      .
  18.
      .
  19.
      .
  20.
       
  21.
       
  22.
      file2.open("test2.txt", ios::in);
  23.
       
  24.
       
  25.
      while (!file2.eof())
  26.
      {
  27.
       
  28.
      .
  29.
      .
  30.
      .
  31.
      .
  32.
       
  33.
      }
  34.
       
  35.
      file2.close();
  36.
       
  37.
       
  38.
      }
  39.
       
  40.
      simiralities<<number;
  41.
      file1.close();
  42.
      simiralities.close();
  43.
       
  44.
      return 0;
  45.
      }

Maybe a file2.clear() would be helpful because upon completion of the first loop the eof bit remains set.

It solved my problem.
thanx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.