Hi the title says everything really. I have a method which reads in a file holding data on the composition of materials (like polythene, or the natural composition of lead) each of these materials is held in a different object and each has a different composition. The make method is called sequentially, as the once the materials which make up the physical system are obtained from another file it is the natural next step. What is written into the list is o/p to a file, then what is actually in the list is op to the end of that file. How do I avoid spillage?
void material::make(char* filename)
{
ifstream fin(filename) ;
ofstream filenameOUT ;
string outfile = (string)filename + "OUT.txt" ;
filenameOUT.open(outfile.c_str()) ;
string ZStr, propStr, isotope ;
vector<float> component(2,0) ;
getline(fin, propStr, '\n') ; //store first entry in file as a str (prop is used to save mem allocation)
lamda = atof(propStr.c_str()) ; //then convert to float so calcs can be done
filenameOUT<<"lamda: "<<lamda<<endl;
while(!fin.eof())
{
getline(fin, isotope, ' ') ; //get the element name
getline(fin, ZStr, '\t') ; //get the Z's and abunancies from the file.
getline(fin, propStr) ;
component[0] = atof(ZStr.c_str()) ;
component[1] = atof(propStr.c_str()) ;
filenameOUT<<"isotope: "<<isotope<<" isotope Z: "<<component[0]<<" proportional amount: "<<component[1]<<endl;
composition.push_back(component) ;
elements.push_back(isotope) ;
}
filenameOUT<<filename<<" is loaded"<<endl;
for(c=composition.begin();c!=composition.end();c++)
{
filenameOUT<<(*c)[0]<<" : "<<(*c)[1]<<" is loaded"<<endl;
}
}
for example, the input file for polyethylene:
.335657
carbon 12 .33333
hydrogen 1 .66667
produces this (which contains isotopes of lead, copper, boron, hydrogen three times and carbon once):
lamda: 0.335657
isotope: carbon isotope Z: 12 proportional amount: 0.33333
isotope: hydrogen isotope Z: 1 proportional amount: 0.66667
poly.txt is loaded
11 : 0.04 is loaded
10 : 0.01 is loaded
12 : 0.31778 is loaded
1 : 0.63332 is loaded
1 : 0.63332 is loaded
204 : 0.014 is loaded
206 : 0.241 is loaded
207 : 0.221 is loaded
208 : 0.524 is loaded
208 : 0.524 is loaded
106 : 0.0125 is loaded
108 : 0.0089 is loaded
110 : 0.1249 is loaded
111 : 0.128 is loaded
112 : 0.2413 is loaded
113 : 0.1222 is loaded
114 : 0.2873 is loaded
116 : 0.0749 is loaded
12 : 0.33333 is loaded
1 : 0.66667 is loaded
any advice much appreciated! (and, yes, it says 'void' as the return. I could wait for a return from each method call before I run the next, but I don't know how to do that.)
I MAY NOT HAVE FOUND THE WAY TO DO THIS ON TINTERWEBS BECAUSE I DON'T REALLY KNOW WHAT THE TERMINOLOGY IS, IF THIS IS THE CASE LINKS TO FIXED PROBLEMS ARE GREAT!