Menu
Coddy logo textTech

Practice Lab #1

Lesson 6 of 23 in Coddy's C++ File Handling course.

challenge icon

Challenge

Easy

*Don't change the code where you are not supposed to*

Try to open 3 files:

  • code.txt
  • script.txt
  • novel.txt

Open them and check each one in that order if it's possible to be open, then print out the message File opened, if it's possible, and Unable to open file otherwise.
After that, close only the files code.txt and novel.txt.

*Print every message in a new line*

Try it yourself

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    //-------------------------
    
    ifstream file_code;
    ifstream file_script;
    ifstream file_novel;
    
    // Enter your code here
    
    
    //-------------------------
    
    // Don't change from here below
    
    if(!file_code.is_open())
        cout << "File closed" << endl;
    else
        cout << "File still opened" << endl;
        
    if(!file_script.is_open())
        cout << "File closed" << endl;
    else
        cout << "File still opened" << endl;
        
    if(!file_script.is_open())
        cout << "File closed" << endl;
    else
        cout << "File still opened" << endl;
    
    return 0;
}

All lessons in C++ File Handling