File writing program OOP
Hi Everybody,
I wrote a program which can read from a file and put these data into several files in a folder. Now my program is working but I wanted to make them into 3 pieces of program as follows,
Makefile.h
Makefile.cpp
main.cpp
But I dont know well how to divide my program. Pls anybody help me to study OOP clearly,
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <dirent.h>
#include <cerrno>
#include <sys/types.h>
#include <sys/stat.h>
#define NMB_MCS 81
using namespace std;
class POV
{
private:
string newDir;
public:
POV(const string & m)
{
newDir = m;
}
void makeFiles() const;
~POV() {}
};
void POV::makeFiles() const
{
string newDir = "POV";
string buff1;
string outname;
string int2string(int);
if ( !mkdir( newDir.c_str(),S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH ) ) {
ofstream outfile[NMB_MCS];
string basename("city");
int i, number_files;
for ( i = 0; i < NMB_MCS; i++) {
outname = newDir + "\\" + basename + int2string(i)+".pov";
outfile[i].open(outname.c_str());
if (!outfile[i]) { // bail out if there was a problem
cout << "Can't open file " << outname << " for writing." << endl;
// return 0; // or you may want to implement some recovery strategy
}
}
int j;
int k=1;
for (j = 0; j < NMB_MCS; j++) {
ifstream input1;
input1.open("driftdata" , ios::in);
outfile[j] << "//This is file number " << j << endl;
int lineno = 0;
while(getline(input1, buff1)) {
if ((++lineno % 82) == k) {
istringstream tmp (buff1);
float a, A;
tmp >> a;
A=a*100;
// cout<< "texture{pigment{rgb< " << A << ", 1, 1 > }}" << endl;
outfile[j]<< "texture{pigment{rgb< " << A << ", 1, 1 > }}" << endl;
}
}
outfile[j]<< "}" << endl;
input1.close();
outfile[j].close();
k++;
cout<< "The file no " << j << " is completed" << endl;
}
}
}
string int2string(int i)
{
#if (NMB_MCS ==81)
int dec = 10;
int nmx = 2;
string result = "00";
#elif (NMB_MCS == 1000)
int dec = 1000;
int nmx = 4;
string result = "0000";
#elif (NMB_MCS == 10000)
int dec = 10000;
int nmx = 5;
string result = "00000";
#endif
int id;
for( int n = 0; n < nmx; n++ )
{
id = i/dec;
result[n] += id;
i -= dec*id;
dec /= 10;
}
return result;
}
int main()
{
POV dir(".");
dir.makeFiles();
return 0;
}
Pls give me some ideas about OOP by changing or improving my program.
Regards,
Sitha.
[3882 byte] By [
sitha] at [2007-11-11 9:57:47]

# 1 Re: File writing program OOP
To divide your file u may put your class into an individual cpp file ; then put your includes and defenitions in a h file and in the first cpp file put an include to this h file ..
for example your Makefile.h file will contains :
#include<fstream>
#...
. and all includes u need and defenitions
and then in the Makefile.cpp u put in the top :
#include "Makefile.h"
and your class
finaly you put the main function into the main.cpp
this is the general idea , you divide a programme to make easy it's develop, avoiding have only a file with more than 200 lines [in this small programme] and many class , includes , defenitions and all that ...
you will have each concept of the programme in a divison and when u want to change and update a class for example u must go to it's specific file only ..
Amahdy at 2007-11-11 20:59:46 >

# 2 Re: File writing program OOP
First of all, you need to be specific about POV assignment and copy construction. Do you want users to be able to copy POV objects? If not, declare the copy ctor and assignment ops as private.
Secondly, your make_files function is very large. You need to split it to smaller, autonomous functions.
Thirdly: don't use hardcoded string literals in your programs. This can cause serious maintenance problems and disables possible localization.
Finally, make sure that the your constructor uses member initailizations instead of plain assignment:
POV(const string & m) :newDir (m) {}
and don't forget to declare yoru constructor as explicit.
Danny at 2007-11-11 21:00:43 >

# 3 Re: File writing program OOP
Hi,
I made 3 files as you all suggested. It is working but I feel it is not a good program. Pls suggest me to improve my program in detail manner. Pls I am a learner of OOP so I need you clear explanation to improve my program.
These are my 3 files,
Pov.h,
# ifndef POV_H
# define POV_H
#include <string>
using namespace std;
class makePOV
{
private:
string newDir;
public:
makePOV(const string & m)
{
newDir = m;
}
void makeFiles() const;
~makePOV() {}
};
#endif
class_pov.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <dirent.h>
#include <cerrno>
#include <sys/types.h>
#include <sys/stat.h>
#include "Pov.h"
#define NMB_MCS 81
using namespace std;
void makePOV::makeFiles() const
{
string newDir = "POV";
string buff;
string buff1;
string buff2;
string buff3;
string outname;
string int2string(int);
if ( !mkdir( newDir.c_str(),S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH ) ) {
ofstream outfile[NMB_MCS];
string basename("city");
int i, number_files;
for ( i = 0; i < NMB_MCS; i++) {
outname = newDir + "\\" + basename + int2string(i)+".pov";
outfile[i].open(outname.c_str());
if (!outfile[i]) { // bail out if there was a problem
cout << "Can't open file " << outname << " for writing." << endl;
// return 0; // or you may want to implement some recovery strategy
}
}
int j;
int k=1;
ifstream input4;
input4.open("Dispset", ios::in);
for (j = 0; j < NMB_MCS; j++) {
ifstream input;
input.open("Povsetting" , ios::in);
ifstream input1;
input1.open("driftdata" , ios::in);
ifstream input2;
input2.open("NODES" , ios::in);
ifstream input3;
input3.open("ELEMENTS" , ios::in);
ifstream input5;
input5.open("damNODES" , ios::in);
ifstream input6;
input6.open("damELEMENTS" , ios::in);
outfile[j] << "//This is file number " << j << endl;
while(getline(input, buff)) {
outfile[j] << buff << endl;
}
while(getline(input2, buff2)) {
outfile[j] << buff2 << endl;
}
int lineno = 0;
while(getline(input1, buff1)) {
if ((++lineno % 82) == k) {
istringstream tmp (buff1);
float a, A;
tmp >> a;
A=a*100;
// cout<< "texture{pigment{rgb< " << A << ", 1, 1 > }}" << endl;
outfile[j]<< "texture{pigment{rgb< " << A << ", 1, 1 > }}" << endl;
}
}
while(getline(input3, buff3)) {
outfile[j] << buff3 << endl;
}
while (getline(input5,buff)) {
outfile[j] << buff << endl;
}
outfile[j] << "texture_list { " <<endl;
outfile[j] << "1638, " << endl;
while(getline(input4,buff)) {
if(buff.find("end")!=string::npos) break;
outfile[j] << buff << endl;
//cout<< buff << endl;
}
outfile[j]<< "}" << endl;
while (getline(input6, buff)) {
outfile[j] << buff << endl;
}
input1.close();
input2.close();
input3.close();
input5.close();
input6.close();
outfile[j].close();
k++;
cout<< "The file no " << j << " is completed" << endl;
}
input4.close();
}
}
string int2string(int i)
{
#if (NMB_MCS ==81)
int dec = 10;
int nmx = 2;
string result = "00";
#elif (NMB_MCS == 1000)
int dec = 1000;
int nmx = 4;
string result = "0000";
#elif (NMB_MCS == 10000)
int dec = 10000;
int nmx = 5;
string result = "00000";
#endif
int id;
for( int n = 0; n < nmx; n++ )
{
id = i/dec;
result[n] += id;
i -= dec*id;
dec /= 10;
}
return result;
}
main.cpp
#include <iostream>
#include "Pov.h"
using namespace std;
int main(void)
{
makePOV direc(".");
direc.makeFiles();
return 0;
}
Pls suggest me how can I improve my program?
Regards,
Sitha.
sitha at 2007-11-11 21:01:46 >
