Declare an fstream object named a_log. Using a_log, check if the file activityLog exists. If the file activityLog does not already exist, open it for writing using a_log; otherwise open the file activityLog2 for writing using a_log.

LANGUAGE: C++

CHALLENGE:

Declare an fstream object named a_log. Using a_log, check if the file activityLog exists. If the file activityLog does not already exist, open it for writing using a_log; otherwise open the file activityLog2 for writing using a_log.

SOLUTION:

fstream a_log;

a_log.open("activityLog",ios::in);
if(a_log.fail())
    a_log.open("activityLog",ios::out);
else
{
    a_log.close(); 
    a_log.open("activityLog2",ios::out);
}