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;
}

You should add some error-checking so that you can discover yourself which part is the problem instead of saying: "doesn't work, please help"

Add a line like this for example: if (!file.is_open()) cout << "file not open"; to check if the file is still open

And to check if the reading was a succes, you might want to change this: getline(file,line); to this: if (!getline(file,line)) cout << "getline failed"; remember: you can (almost) never put to much errorchecking in a program!

You should add some error-checking so that you can discover yourself which part is the problem instead of saying: "doesn't work, please help"

Add a line like this for example: if (!file.is_open()) cout << "file not open"; to check if the file is still open

And to check if the reading was a succes, you might want to change this: getline(file,line); to this: if (!getline(file,line)) cout << "getline failed"; remember: you can (almost) never put to much errorchecking in a program!

ok,I put those checkings but there still seems no problem,but no output too...
I tried char* instead of strings but it still didnt work...

If your really put in all the error-checking, then post the new code

Does your console close very fast? If yes: add a cin.get() at the end of your program ( before return 0; )

ok,I put those checkings but there still seems no problem,but
no output too...
I tried char* instead of strings but it still didnt work...

One fix to the reading problem is to re-open the file, or alternatively you might implement
something similar to seekg() to set position of the get pointer.

int main(){
	string filename = "example.txt";
	Handler fileHandler(filename);
	
	
	fileHandler.openFile();
	string str = "hello";
	fileHandler.writeLine(str);

        // re-open the file so that the read occurs
        // at the beginning of the file
	fileHandler.openFile();

	string str2;
	str2 = fileHandler.readLine();
	cout << str2 << endl;

	return 0;
}

One fix to the reading problem is to re-open the file, or alternatively you might implement
something similar to seekg() to set position of the get pointer.

int main(){
	string filename = "example.txt";
	Handler fileHandler(filename);
	
	
	fileHandler.openFile();
	string str = "hello";
	fileHandler.writeLine(str);

        // re-open the file so that the read occurs
        // at the beginning of the file
	fileHandler.openFile();

	string str2;
	str2 = fileHandler.readLine();
	cout << str2 << endl;

	return 0;
}

yes you are right,I noticed that after I posted the first code..
the problem is not that,when I did these things in main,everything's good,but when I want to implement it in a class and put functions,it didnt work...

If your really put in all the error-checking, then post the new code

Does your console close very fast? If yes: add a cin.get() at the end of your program ( before return 0; )

#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 &);
	bool 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;
}
bool Handler::openFile(){
	closeFile();
	file.open(fileName.c_str(),ios::in | ios::out );
	if(file.is_open())
		return true;
	return false;
}
string Handler::readLine(){
	string line = "failed";
	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);
	
	
	if(!fileHandler.openFile())
		cout << "file is not opened"  << endl;
	string str = "hello";
	fileHandler.writeLine(str);
	string str2;
	fileHandler.closeFile();
	fileHandler.openFile();
	if(!fileHandler.openFile())
			cout << "file is not opened"  << endl;
	str2 = fileHandler.readLine();
	cout << str2 << endl;

	return 0;
}

the console output is:
file is not opened
file is not opened
failed

why couldnt this file be created?

A couple of things

1) When opening the file, in addition to the ios::out|ios::in flags, specify one of: app, ate or trunc flag.

2) When closing the file, close it only if it is open, i.e.

if(file.is_open())
    file.close();

Also you probably want to understand the error state flags and the related member functions.

I tried ios::ate,ios::app,ios::trunc,
only trunc was successful in creating file but not in reading or in writing

when there is already a file,the program works,but if it tries to create the file(which the "open" function is supposed to do),the program fails.

finally I found a solution which I designed before.To use two objects for file operations-an ifstream object for input and an ofstream object for output- worked well.But I still dont know why only one fstream object wasnt able to work.Maybe I couldnt manage,but I will continue with this new approach.If you can see what the matter with the fstream is,I will be pleased to know...

#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 &);
	bool openFileToRead();
	bool openFileToWrite();
	string readLine();
	void writeLine(string &);
	void closeFile();
	
private:
	string fileName;
	ifstream fileIn;
	ofstream fileOut;
};

#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;
}
bool Handler::openFileToRead(){
	closeFile();
	fileIn.open(fileName.c_str());
	if(fileIn.is_open())
		return true;
	return false;
}
bool Handler::openFileToWrite(){
	closeFile();
	fileOut.open(fileName.c_str());
	if(fileOut.is_open())
		return true;
	return false;
}
string Handler::readLine(){
	string line = "failed";
	getline(fileIn,line);
	return line;
	
}
void Handler::writeLine(string &line){
	fileOut << line << endl;
}
void Handler::closeFile(){
	if(fileOut.is_open())
		fileOut.close();
	if(fileIn.is_open())
		fileIn.close();
	
}
#include "Handler.h"

int main(){
	string filename = "example.txt";
	Handler fileHandler(filename);
	
	
	if(!fileHandler.openFileToWrite())
		cout << "file is not opened"  << endl;
	string str = "hello";
	fileHandler.writeLine(str);
	string str2;
	if(!fileHandler.openFileToRead())
		cout << "file is not opened"  << endl;
	str2 = fileHandler.readLine();
	cout << str2 << endl;

	return 0;
}

Which compiler/OS are you using?
The (modified) code worked for me (Visual Studio 2005), but NOT with GCC 3.4.2.

Which compiler/OS are you using?
The (modified) code worked for me (Visual Studio 2005), but NOT with GCC 3.4.2.

I am using Eclipse C/C++ Development 4.0.3 which uses GCC 3.4.5 on Vista OS

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.