I'm learning file handling in C++. I was implementing the exact same code in my Code::Blocks 20.03 as given in one of the programs of the book, but it's displaying no output after line 26, i.e.
cout<<"\nReading the file contents: ";
I've figured maybe these lines are erraneous, but I can't debug how:
while(file){
file.get(ch);
cout<<ch;
}
Here is the full code:
#include <iostream>
#include <fstream>
#include <cstring>
#include <stdlib.h>
using namespace std;
int main()
{
char String[80];
cout<<"Enter a string: ";
cin>>String;
int len = strlen(String);
fstream file;
cout<<"Opening the 'TEXT' file and storing the string in it.\n\n";
file.open("TEXT",ios::in|ios::out);
for(int i=0;i<len;i++)
file.put(String[i]);
file.seekg(0);
char ch;
cout<<"\nReading the file contents: ";
while(file){
file.get(ch);
cout<<ch;
}
file.close();
return 0;
}