Answer to Question #199287 in C++ for Dilip Kushwah

Question #199287

Explain f.open(“abc”,ios::app | ios::nocreate) and f.seekg(-20,ios::cur) , eof() and while(fin),

tellg() function with example


1
Expert's answer
2021-05-27T23:09:27-0400
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
    fstream f;
    f.open("abc", ios::in); //f.open(“abc”,ios::app | ios::nocreate) nocreate is not defined in std io
    if(f) cout<<"File opened successfully\n";
    else cout<<"File was not created\n";


    f.open("abc", ios::out); //create a file
    f<<"Something is being transferred\nwritten\nin\nthis\nfile";
    f.close();
    string s;
    f.open("abc", ios::in);
    cout<<"Get pointer is at position "<<f.tellg()<<endl;
    getline(f, s);
    cout<<"Get pointer is at position "<<f.tellg()<<endl;
    f.seekg(-20, ios::cur);
    cout<<"Get pointer is at position "<<f.tellg()<<endl;


    char c;
    while(f.get(c)){}
    if(f.eof()) cout<<"Reached End of file!\n";
    else cout<<"Not end of 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