Vertical File Output

Vertical File Output

Write a program that reads a file named example.txt character by character using the get() function from the std::ifstream class. The program should print each character to the console, with each character appearing on its own line.

Example file:

Hello, world!

Example output:

H
e
l
l
o
,

w
o
r
l
d
!

Answer

#include <iostream>
#include <fstream>

int main() {
    std::ifstream inFile("example.txt");
  
    if (!inFile) {
        std::cerr << "Error: Could not open the file." << std::endl;
        return 1;
    }

    char ch;
    while (inFile.get(ch)) {
        std::cout << ch << std::endl;
    }

    inFile.close();
    return 0;
}
Questions