A simple code to open a file and write something and read from a file...(C++)
But something is wrong here..
Let me know...what it is!!!
Comment below..!!!!
#include<iostream>
#include<sstream>
#include<fstream>
using namespace std;
int main()
{
fstream fobj;
string filename = "/home/ferdose/Desktop/file.txt";
string str;
fobj.open("filename",ios::in|ios::out);
if(!fobj) cerr<<"Can't open"<<endl;
fobj>>str;
fobj<<"hey"<<endl<<"hello";
cout<<str;
}
But something is wrong here..
Let me know...what it is!!!
Comment below..!!!!
#include<iostream>
#include<sstream>
#include<fstream>
using namespace std;
int main()
{
fstream fobj;
string filename = "/home/ferdose/Desktop/file.txt";
string str;
fobj.open("filename",ios::in|ios::out);
if(!fobj) cerr<<"Can't open"<<endl;
fobj>>str;
fobj<<"hey"<<endl<<"hello";
cout<<str;
}



what does fstream fobj do?
ReplyDeletefstream is a class of header file...
ReplyDeletefobj.open("filename",ios::in|ios::out);
ReplyDeleteoops i dint get this........
In C, for opening a file, we write fopen("somt.txt","r")
DeleteHere,
this is the way of opening a file in C++....
oh...
Deleteand fobj>>str..
here fobj is an object of class fstream...then object accesing a data of main().....
how come??
That is the beauty of C++ handling of files...
ReplyDeleteOverloading << and >> operators to function as cin and cout for Files...