how to use ifstream in header file

I write header file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class FileOperation{
	public: FileOperation();
	public: bool nextLine();

	public: void setFileName(char* fName);
	public: char* getFileName();
	public: void setFileR();
	public: void closeFileR();
	private: std::ifstream* fileR;
	private: char* fileName[1];
	
};

inline void FileOperation::setFileName(char* fName){
	FileOperation::fileName[0] = fName;
} 

inline char* FileOperation::getFileName(){
	return FileOperation::fileName[0];
}

inline void FileOperation::setFileR(){
	std::ifstream fileR(FileOperation::getFileName());
	FileOperation::fileR = &fileR;
}


*.cpp file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void FileOperation::closeFileR(){
	(*FileOperation::fileR).close();
}

bool FileOperation::nextLine(){
	if ((*FileOperation::fileR).is_open())
		if ((*FileOperation::fileR).eof())
			return false;
	return true;
}

std::string FileOperation::getLine(){
        strd::string line2 = "";
	std::getline((*FileOperation::fileR), line2);
	return line2;
}


if use nextLine() or closeFileR method i have error. If i use getLine() (my method) in header in setFileR() it work good.
1
2
3
4
5
6
std::string line;
	if ((*FileOperation::fileR).is_open()){
		while (!(*FileOperation::fileR).eof()){
			std::cout << FileOperation::getLine() << endl;
		}
	}

Pleas tell me how i must write method nextLine() and closeFileR() right?
Last edited on
You know? You can just do this:
1
2
3
4
5
6
7
8
9
10
11
12
class FileOperation{
	//classes give their members private access by default
	std::ifstream* fileR;
	char* fileName[1];
public: 
	FileOperation();
	bool nextLine();
	void setFileName(char* fName);
	char* getFileName();
	void setFileR();
	void closeFileR();
};


As for your question,
1
2
3
void FileOperation::closeFileR(){
	this->close();
}
Last edited on
If use your samples i have error

error C2039: 'close' : is not a member of 'FileOperation'

i can compile progam with code
1
2
3
void FileOperation::closeFileR(){
	this->fileR->close();
}


but if i run program i want close file then i have error
I now where is bug. In setFileR i make localy variable, whose is killed when i end use method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class FileOperation{
	public: FileOperation();
	public: bool nextLine();

	public: void setFileName(char* fName);
	public: char* getFileName();
	public: void setFileR();
	public: void closeFileR();
            // must make variable ifstream
	private: std::ifstream fileR;
	private: char* fileName[1];
	
};

inline void FileOperation::setFileName(char* fName){
	FileOperation::fileName[0] = fName;
} 

inline char* FileOperation::getFileName(){
	return FileOperation::fileName[0];
}

inline void FileOperation::setFileR(){
              // changed
	std::ifstream fileR(FileOperation::getFileName());
	FileOperation::fileR2.copyfmt(fileR);
}


1
2
3
inline void FileOperation::closeFileR(){
	this->fileR2.close();
}


It's work now ;-)
Last edited on
error C2039: 'close' : is not a member of 'FileOperation'
My bad.

but if i run program i want close file then i have error
You have to initialize the pointer to a valid object.
http://www.cplusplus.com/doc/tutorial/dynamic/
Oki, i have new code it do what i want ;-)

Only change this method
1
2
3
inline void FileOperation::setFileR(){
	FileOperation::fileR.open(FileOperation::getFileName());
}


I lerning new about c++ evry day ;-)
Last edited on
Topic archived. No new replies allowed.