hello,I would like to create a class for basic file operations-just reading and writing a line.There seems no error in my program but it doesn't output anything.I think there is a problem with the functions for reading and writing.Please help...
#ifndef HANDLER_H_
#define HANDLER_H_
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Handler
{
public:
Handler(string &);
~Handler();
string getFileName()const;
void setFileName(string &);
void openFile();
string readLine();
void writeLine(string &);
void closeFile();
private:
string fileName;
fstream file;
};
#endif /*HANDLER_H_*/
#include "Handler.h"
#include <iostream>
Handler::Handler(string &fileName){
setFileName(fileName);
}
Handler::~Handler(){
}
string Handler::getFileName()const{
return fileName;
}
void Handler::setFileName(string &fileName){
this->fileName = fileName;
}
void Handler::openFile(){
closeFile();
file.open(fileName.c_str(),ios::in |ios::out );
}
string Handler::readLine(){
string line;
getline(file,line);
return line;
}
void Handler::writeLine(string &line){
file << line << endl;
}
void Handler::closeFile(){
file.close();
}
#include "Handler.h"
int main(){
string filename = "example.txt";
Handler fileHandler(filename);
fileHandler.openFile();
string str = "hello";
fileHandler.writeLine(str);
string str2;
str2 = fileHandler.readLine();
cout << str2 << endl;
return 0;
}