0
0
C++programming~10 mins

File input using ifstream in C++ - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to open a file named "data.txt" for reading.

C++
#include <fstream>
#include <iostream>

int main() {
    std::ifstream file;
    file.[1]("data.txt");
    if (!file) {
        std::cout << "Failed to open file." << std::endl;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Awrite
Bopen
Cread
Dclose
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'close' instead of 'open' to open the file.
Trying to use 'read' or 'write' directly to open the file.
2fill in blank
medium

Complete the code to read a single word from the file into the variable 'word'.

C++
#include <fstream>
#include <iostream>
#include <string>

int main() {
    std::ifstream file("data.txt");
    std::string word;
    if (file >> [1]) {
        std::cout << "Read word: " << word << std::endl;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Adata
Binput
Cfile
Dword
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Trying to read into 'input' or 'file' which are not variables for storing the word.
Using 'data' which is not declared.
3fill in blank
hard

Fix the error in the code to properly check if the file was opened successfully.

C++
#include <fstream>
#include <iostream>

int main() {
    std::ifstream file("data.txt");
    if ([1]) {
        std::cout << "File opened successfully." << std::endl;
    } else {
        std::cout << "Failed to open file." << std::endl;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Afile.is_open()
Bfile
C!file
D!file.is_open()
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using '!file' which checks if the file failed to open, but logic is reversed.
Using '!file.is_open()' which is true when file is not open.
4fill in blank
hard

Fill both blanks to read all words from the file and print them.

C++
#include <fstream>
#include <iostream>
#include <string>

int main() {
    std::ifstream file("data.txt");
    std::string [1];
    while (file >> [2]) {
        std::cout << [2] << std::endl;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Aword
Bfile
Cinput
Ddata
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using different variable names for declaration and reading.
Using 'file' as a variable to store words.
5fill in blank
hard

Fill all three blanks to open a file, read integers, and sum them.

C++
#include <fstream>
#include <iostream>

int main() {
    std::ifstream [1]("numbers.txt");
    int [2], sum = 0;
    while ([1] >> [2]) {
        sum += [2];
    }
    std::cout << "Sum: " << sum << std::endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
Afile
Bnum
Dnumber
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using different names for the file stream in declaration and reading.
Using 'number' instead of 'num' for the integer variable.