Reading Char by Char
Lesson 9 of 23 in Coddy's C++ File Handling course.
We perform reading char by char by opening the file in input mode and then using a char variable to store each character separately.
ifstream my_file;
my_file.open("example.txt");
char c;
while(!my_file.eof()) {
my_file.get(c);
cout << c << endl;
}As you can see, this is how we read char by char, we first create an ifstream, and then we create a char named c. We again use a while loop to cycle through the file until we reach the end-of-file.
Challenge
EasyOpen the file named message.txt. This time read character by character but use an if-statement to skip all of the numbers in the file. Print everything separating it with a whitespace.
Try it yourself
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
// Enter your code here
return 0;
}All lessons in C++ File Handling
3Reading from Files
Reading from a fileReading Line by LineReading Char by CharReading Variable by VariablePractice Lab #2