Working with Directories in C++

I am new in C++ programming, i am trying to add some codes to my programme, to enable it process several input files automatically during runtime and give results in several output files relating to the input files.My idea is to prepare several text files with the required informations and put them in one folder/ directory which i can use as a single input, instead of typingin each text file as i am now working with my programme. Any body can render me a helping hand on how to go about the idea in C++ codes.
below is part of the programme i am developing.
#include <iostream>
#include <conio.h>
#include <math.h>
#include <windows.h>
#include <vector>
#include "epanet2.h"
#include <stdlib.h>
#include <fstream>
#include <istream>
#include <sstream>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
using namespace std;
#define PIPE_CLOSED 0
#define PIPE_OPEN 1
#define MAX_PATH_LEN 500 /* Maximum number of Chars in a datafile line */
#define MAX_LABEL_LEN 25 /* Maximum length of an ID or Label */
char file[MAX_PATH_LEN];
char rept[MAX_PATH_LEN];
int Numinpfile;
int Numpipes;
int Numnodes;
double Demand_loss;
double trans_ADF;
double pi;
double Loss_Demand;
double cost;
ofstream outputfile;

vector<string> nodes;
vector<string> pipes;
vector<float> Length;
vector<float> Diameter;
vector <float>Flow;
vector <float>Headloss;
vector<float> Demand;
vector<float> break_Demand;
vector <float> break_Headloss;
vector <float> break_Flow;
vector<float> Demandwithoutme;
vector<double>ADF;
vector<double>DemandLoss;
vector<double> Cost;
vector <double> Beta;
vector <double> Pi;
vector <double> Reliability;

void prepare_Input_param(char* filename);
int Loss_demand();
void epanetInit();
void epanetClose();
double pipes_cost();
void pipe_pi_val();
double ind_reliability ();

int main(int argc,char *argv[]){
outputfile.open ("Loss_Demand_output_Net33.xls");
outputfile << "\nPipes ID " << "\t" << " Diameter(mm)" << "\t" << "Length(m)"<< "\t"<< "Flow_reg(l/s)" << "\t" << "Headloss_reg(m)"<< "\t" << " ADF " << "\t"<< " DemandLoss " << "\t" << "Pipe cost(eur) "<< "\t"<< "Pipe Beta"<<"\t"<< "Pipe pi" << "\t" << " Relia_ind " << endl;


if(argc<1){
cout << "You are requested to supply the folder containing networks parameters related to epanet !.>\n";
exit(1);
}
char* dirname=argv[1]; // Seperately copy argv[1] (first argument) to variable dirname
cout << "dirname : "<< dirname << endl;
char* argv_[MAX_PATH_LEN];
argv_[0]=argv[0]; // argv[0] is the calling program name, copy this as it is.

for(int i=1;i<argc-1;i++){ // then copy the rest (argv[2], argv[3],...) of arguments to new arrays
cerr << argv[i+1]<<endl;
argv_[i]=argv[i+1];
}
argc--; // argc should be in the descending order.

Organise_Input_data(dirname);
cout << "Networks Inputfolder" <<directory<< " Is organised for use ....\n" << endl;

prepare_Input_param(filename);

cout << " Network Inputfile " << file << " is imported and prepared in order to be analysed...\n" << endl;

Loss_demand();
pipes_cost();
pipe_pi_val ();
ind_reliability ();

outputfile.close();
}

/** A function to open, read and close a directory with networks input files **/

void Organise_Input_data(char *dirname)

int getdir (string dir, vector<string> &files)

{
// open the directory

DIR *dp;
struct dirent *dirp;
if((dp = opendir(dir.c_str())) == NULL) {
cout << "Error(" << errno << ") opening " << dir << endl;
return errno;
}
while ((dirp = readdir(dp)) != NULL) {
files.push_back(string(dirp->d_name));
}
closedir(dp);
return 0;
}

/** read the text file specified by filename argument and obtain epanet related parameters */
void prepare_Input_param(char* filename)
{
//open the file
ifstream myfile (filename);
if(!myfile.is_open()){ // This is very useful .
cout << "I can not open the file:"<<filename <<" I cannot continue i quit!!\n";
exit(1);
}
myfile >> file; //read the name of the file
myfile >> rept; //read the name of the report file
myfile >> Numpipes; //number of pipes

for(int i=0;i<Numpipes;i++){ // read pipe id's
char tmp[MAX_LABEL_LEN];
myfile >> tmp;
pipes.push_back(tmp);
}

myfile >> Numnodes; //Number of junctions
for(int i=0;i<Numnodes;i++){//read nodal id's
char tmp[MAX_LABEL_LEN];
myfile >> tmp;
nodes.push_back(tmp);
}

}

int Loss_demand(){
int ret;
float Cumdemand;
float Htotal_reg;
long time, tstep;
float break_dwm=0;
float headloss_irreg=0;
float flow_irreg=0;

// Run the first simulation without breaking the pipe
epanetInit();

ret=ENopenH_wrap();
cout << "EN open : "<< ret << endl;

ret=ENinitH_wrap(0);
ret=ENrunH_wrap(&time);
cout << " This is the first epanet running without breaking of pipes ...";

for(int i=0;i<Numpipes;i++) {
int index = -1;

char tmp[MAX_LABEL_LEN];
strcpy(tmp,pipes[i].c_str());

/* Retrieve pipes diameter */
float diameter;
ret=ENgetlinkindex_wrap(tmp,&index);
ret=ENgetlinkvalue_wrap(index,EN_DIAMETER, &diameter);
Diameter.push_back(diameter);

// Retrieve pipes length
float length;
ret=ENgetlinkindex_wrap(tmp,&index);
ret=ENgetlinkvalue_wrap(index,EN_LENGTH, &length);
Length.push_back(length);

/* Retrieve regular condition flow in pipes */
float flow_reg;
ret=ENgetlinkindex_wrap(tmp,&index);
ret=ENgetlinkvalue_wrap(index,EN_FLOW, &flow_reg);
Flow.push_back(flow_reg);


/* Retrieve pipes head losses and calculate the total headloss in the network under regular operating conditon */

float headloss_reg;
Htotal_reg=0;
ret=ENgetlinkindex_wrap(tmp,&index);

ret=ENgetlinkvalue_wrap(index,EN_HEADLOSS, &headloss_reg);
Headloss.push_back (headloss_reg);

Htotal_reg+=headloss_reg;

}

//Retrieve Nodal demand values calculate the total demand of the network and put this into variable
Cumdemand=0;

for(int i=0;i<Numnodes;i++){
int index=-1;
float value;
char tmp[MAX_LABEL_LEN]; // convert C++ string to C style char*
strcpy(tmp,nodes[i].c_str());

ret=ENgetnodeindex_wrap(tmp,&index);
//if(ret!=0){outputfile << "At ENgetnodeindex Epanet Error returned " << ret << endl;}

ret=ENgetnodevalue_wrap(index,EN_DEMAND,&value);
Demand.push_back(value);
//if(ret!=0){outputfile << "At ENgetnodevalue Epanet Error returned " << ret << endl;}

if(value>=0){
Cumdemand = Cumdemand + value ;
}
}
ret=ENcloseH_wrap();


/*Break pipes, run single time simulation and retrieve demands */

for (int i=0;i<Numpipes;i++){
cout << "pipes : "<< Numpipes << endl;

float Demandwithoutme=0;
float Htotal_irreg=0;
int index = -1;
char tmp[MAX_LABEL_LEN]; // Convert a C++ string to a Cstyle char*
strcpy(tmp,pipes[i].c_str()); // Epanet written in C language which doesnot accept strings
ret=ENgetlinkindex_wrap(tmp,&index);
cout << " Get-pipes-index Error returned " << ret << endl;
cout << " Breaking a pipe now " << ret << endl;

ret=ENsetlinkvalue_wrap(index,EN_INITSTATUS,PIPE_CLOSED);
cout << " setlinkvalue Error returned " << ret << endl;

ret= ENopenH_wrap();
ret= ENinitH_wrap(10);
do
{
ret=ENrunH_wrap(&time);
cout << "At ENrunH_wrap Error returned " << ret << endl;

for (int i=0; i<Numnodes; i++)
{
cout << "Numnodes : "<< Numnodes << endl;
int index=-1;
char tmp[MAX_LABEL_LEN]; // convert C++ string to C style char*
strcpy(tmp,nodes[i].c_str());
ret=ENgetnodeindex_wrap(tmp,&index);
if(ret!=0){outputfile << "At ENgetnodeindex_wrap Epanet Error returned " << ret << endl;}

ret=ENgetnodevalue_wrap(index,EN_DEMAND,&break_dwm);
//if(ret!=0){outputfile << "At ENgetnodevalue_wrap Epanet Error returned " << ret << endl;}
break_Demand.push_back(break_dwm);
Demandwithoutme+=break_dwm;
}
ret= ENnextH_wrap(&tstep);
}
while (tstep=0);

// Calculate system ADF (ADFnet).

trans_ADF = Demandwithoutme/Cumdemand;

ADF.push_back(trans_ADF);

Loss_Demand = (1- trans_ADF);
DemandLoss.push_back(Loss_Demand);

ret=ENsetlinkvalue_wrap(index,EN_INITSTATUS,PIPE_OPEN);

cout << " setlinkvalue Error returned " << ret << endl;

}

ret=ENcloseH_wrap();
return Loss_Demand;

}

void epanetInit()
{
int ret;
ret=ENopen_wrap( file, rept,"");
if(ret!=0){outputfile << "At ENopen_wrap Epanet Error returned " << ret << endl;}
}
void epanetClose()
{
int ret;
ret=ENclose_wrap();
if(ret!=0){outputfile << "At ENclose_wrap Epanet Error returned " << ret << endl;}
}
Use code tags ;)

Otherwise - your code gets unreadable.
Topic archived. No new replies allowed.