The
& refers to a reference, which is a way of passing parameters to things such as functions.
I won't bother explaining references fully, as Narue does that nicely
here.
By writing a function that takes a std::ifstream object, you can specify the source of the input.
For example:
#include <iostream>
#include <fstream>
#include <string>
void readNLines( int n, std::ifstream &in ) {
std::string line;
for ( int x = 0; x < n; x++ ) {
std::getline( in, line );
std::cout << x + 1 << ": " << line << std::endl;
}
}
int main() {
// Make sure in real code, you check for success or failure
// when opening files.
// Open this file for reading
std::ifstream input( "example.cpp" );
readNLines( 3, input );
}