Answer to Question #272896 in C++ for Amit

Question #272896

write a program to read the last word from a file and print to output screen.


1
Expert's answer
2021-11-29T00:00:47-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS