Nested while loops

I am trying to read in from two files and write out all posaible out comes.

File "A" has two lines and File "B" as two lines it would print out this:

lineA1-LineB1
LineA1-LineB2
LineA2-LineB1
LineA2-LineB2

What i am getting is just the first part:

LineA1-LineB1
LineA1-LineB2

I do not know why it will not start the while loop on the second round.

Part of the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
while ( FileA.good() )
    {	
      
		getline (FileA,line);
		
		while( FileB.good() )
		  {
			  
			  getline (FileB,lineB);

			  cout << line <<"-"<< lineB << endl;
		  }
	  }


thanks for any help.
once you have read all lines of file B in the first outer cycle, you cannot read any more. the solution would be either to use istream::seekg or to read everything into vectors before printing the combinations.
I am having trouble with the istream::seekg.

how do i get File "A" line to print everytime?
i am getting:
LineA1-LineB1
LineB2
LineB3
LineA2-LineB1
LineB2
LineB3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
while ( FileA.good() )
    {	
      
		getline (FileA,line);
		int length;
		char * buffer;

		ifstream is;
		is.open ("FileB.txt", ios::binary );

		// get length of file:
		is.seekg (0, ios::end);
		length = is.tellg();
		is.seekg (0, ios::beg);
		
		// allocate memory:
		buffer = new char [length];

		// read data as a block:
		is.read (buffer,length);
		is.close();

		cout << line << "-" ;
		cout.write (buffer,length);
		cout << endl;

		delete[] buffer;
		FileB.close();
	}
Why did you copy that example?

I'll try to explain. Here's your code:
1
2
3
4
5
6
7
8
while ( FileA.good() ){	
    getline (FileA,line);

   while( FileB.good() ){
      getline (FileB,lineB);
      cout << line <<"-"<< lineB << endl;
   }
}


If we unroll the outer loop, we can see how FileB's get pointer is changing.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//cycle 1:
getline (FileA,line);

//FileB was just opened so get ptr points to its beginning.
while( FileB.good() ){
   getline (FileB,lineB);//get ptr moves forward
   cout << line <<"-"<< lineB << endl;
}
//the inner loop ends when an error flag is set due to reaching end of file

//cycle 2:
getline (FileA,line);

//FileB has an error flag set, so this loop will not be entered. what can we do?
// first clear the flag (forgot this the last time) and then move the get pointer to the beginning.

//this is done with
FileB.clear();
FileB.seekg(std::ios::beg, 0);
//now FileB is readable again

while( FileB.good() ){
   getline (FileB,lineB);//get ptr moves forward
   cout << line <<"-"<< lineB << endl;
}
//the inner loop ends when an error flag is set due to reaching end of file

//... repeat as much as you like. 


Now you just have to roll this back. I put that .clear, .seekg in a slightly awkward place (that is, I didn't show it on the first cycle), but you can put it anywhere before the second input operation. No harm can come from it.
I get it now.

Thank you for your help.
Topic archived. No new replies allowed.