Open Two Files Using Same In Stream

Hello! I am working on a lab for my class, and I am having trouble with File I/O. I need to open two files, "datafile1.txt" and "datafile2.txt", and pass them to a function that will get the first line as an int variable. An example text file (datafile1.txt) is:
4
Dana Smith F
River Jones M
Jordan Pollic F
Liska Fanta M

So I pass my stream to my function, and I don't know where to go from here in terms of differentiating between the two functions. This is what I have right now and I know it's not right, but I'm not sure how to fix it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()  {
    // Declare variables
    dataRecord record;
    // Open files for input
    std::ifstream inFile1;
    std::ifstream inFile2;
    if(!inFile1.is_open())   {
        std::cout << "Sorry, could not open file." << std::endl;
        exit (0);
        }
    if(!inFile2.is_open())   {
        std::cout << "Sorry, could not open file." << std::endl;
        exit (0);
        }
    inFile1 = inFile1.open("datafile1.txt");
    inFile2 = inFile2.open("datafile2.txt");

    // Make function calls to get size to use in new statement to create dynamic array
    int numRecordsInFile1 = readHeader(inFile1);
    int numRecordsInFile2 = readHeader(inFile2);
    
    // Get dynamic memory
    int totalRecord = numRecordsInFile1 + numRecordsInFile2;
    record * ArrayPtr = new dataRecord[totalRecord];


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int readHeader(std::ifstream inFile)    {
    int NumRecordsInFile1 = 0;
    int NumRecordsInFile2 = 0;
    if(inFile == inFile1)    {
        inFile >> numRecordsInFile1;
        return numRecordsInFile1;
        }
    if(inFile == inFile2)    {
        inFile >> numRecordsInFile2;
        return numRecordsInFile2;
        }
    else    {
        exit(0);
        }
    }

Do not doublepost (create multiple threads about same problem).
That simply wastes everyone's time, including yours.
Other thread http://www.cplusplus.com/forum/beginner/242592/
Topic archived. No new replies allowed.