DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: File writing program OOP

  1. #1
    Join Date
    Nov 2006
    Posts
    14

    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,

    Code:
    #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.

  2. #2
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    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 ..
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    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 Kalev

  4. #4
    Join Date
    Nov 2006
    Posts
    14
    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,
    Code:
    # 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
    Code:
    #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
    Code:
    #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.

  5. #5
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    As Danny told u it's better to divide your "makeFile()" function ; that will help u also to use specific options in your function ..

    what makes for u the sence of "not good" ?
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  6. #6
    Join Date
    Nov 2006
    Posts
    14
    Hi,
    I dont understand well how to divide my makefile function. Pls kind enough to give me some idea to divide that function.

    Regards,
    Sitha.

  7. #7
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    Those consecutive for while loops could be in separate sub functions .. also they could still member of the class ..
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

Similar Threads

  1. Reading and writing lines from a text file...
    By Jenks in forum VB Classic
    Replies: 6
    Last Post: 05-24-2005, 02:22 PM
  2. Replies: 1
    Last Post: 11-26-2002, 01:56 PM
  3. Replies: 2
    Last Post: 03-07-2002, 08:49 AM
  4. Please help with running executable vb program.
    By Gordon Reichhardt in forum VB Classic
    Replies: 2
    Last Post: 01-08-2002, 11:06 AM
  5. DefaultIcon in a file asociated with my program
    By MaRiŲ in forum VB Classic
    Replies: 2
    Last Post: 10-19-2001, 04:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center