Explain f.open(“abc”,ios::app | ios::nocreate) and f.seekg(-20,ios::cur) , eof() and while(fin),
tellg() function with example
#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;
}
Comments
Leave a comment