write a program to read the last word from a file and print to output screen.
#include <iostream>
#include <fstream>
using namespace std;
int main(){
char filename[50];
cout<<"Enter filename to read: ";
cin>>filename;
ifstream file(filename, ios::in);
if(file){
char word[50];
while(!file.eof()){
file>>word;
}
cout<<word;
file.close();
}
else{
cout<<"error opening file\n";
}
return 0;
}
Comments
Leave a comment