code structure

Note:Updated code
Question change: is the structure easy to understand/test and if not what changes should be made?

.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef File_scanner
#define File_scanner
#include<iostream>
#include<fstream>
#include<sstream>

class scanner {
private:
	std::ifstream file;
	std::stringstream File_copy;
	int File_size;
public:
	void set_file_size(std::string& File_directory);
	void set_file_copy(std::string& File_directory);
	int get_file_size();
	std::string get_file_copy();
};

#endif 


.cpp
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
28
29
30
31
32
33
#include "File_scanner.h"
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>

void scanner::set_file_size(std::string& File_directory) {
	file.open(File_directory, std::ios::in | std::ios::ate);
	if (file.is_open()) {
		File_size = static_cast<int>(file.tellg());
		file.close();
	}
	else std::cout << "Unable to open the file to set size." << std::endl;
}

void scanner::set_file_copy(std::string& File_directory) {
	file.open(File_directory, std::ios::in);
	if (file.is_open()) {
		File_copy << file.rdbuf();
		file.close();
	}
	else std::cout << "Unable to open the file for copy operation." << std::endl;
}

int scanner::get_file_size() {
	return File_size;
}

std::string scanner::get_file_copy() {
	return File_copy.str();
}

Last edited on
ok thanks!
No. This is a very small class. It is straight-forward and easy to follow. No reason to separate the functions into different .cpp files.

Creating separate .cpp files for each function makes it harder to follow. The usual convention is one class per .cpp file.

Last edited on
NOTE: i changed the code and it works perfectly which changes the question a bit: is the structure ok now ?
Topic archived. No new replies allowed.