I need to read from different txt files, that have data of different type, one txt file has int data, other, double data, etc. using templates.
For the int data the program works perfectly, but gives me weird numbers with floats, and for double cant even read the data,
can someone point me to the right direction
i declared a previously template like
1 2
|
template <class myType>
myType *dataSet; //dynamic array using the template
|
thanks
this section has the functions for reading the data, and printing out ,while with int works, with float not all the values are correct, and double data gives core dumped error
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
bool readDataFile(){
ifstream inFile ;
inFile.open(fileName);
inFile.ignore(1000, '\n');
inFile.ignore(1000, '\n');
inFile.ignore(1000, '\n');
inFile.ignore(1000, '\n');
inFile >> setSize;
dataSet = new myType[setSize];
for(int i =1; i <= setSize; i++) {
inFile >>dataSet[i];
}
delete [] dataSet;
inFile.close();
}
bool setFileName(string fileName){
if(fileName.size() > 4 && (fileName.substr(fileName.size() - 3) == "dat" || fileName.substr(fileName.size() - 3) == "txt") /*||(access(fileName.c_st r(), F_OK))*/) {
this->fileName= fileName;
return true;
}
else {
cout << "File does not exist"<< endl;
return false;
}
}
void printDataSet() const{ //printDataSet() function should print data items of type myType with nine (9) values per line.
for (int i = 1; i <= setSize; i++) {
cout <<setw(5)<<(dataSet[i] << " ";
if(i%9 ==0)
cout << endl;
}
cout<<endl;
}
|
here i want to read the data from a txt file, as double but for some reason, gives me core dumped error, while with the int data, works perfect.
1 2 3 4 5 6 7 8 9 10 11 12 13
|
statsType <double> set3;
double min3, max3, med3, sum3, ave3;
double sStd3, pStd3, sk3, fp3;
cout << stars << endl;
cout << "Data Set 3 - double" << endl << endl;
//set3.readFileName();
set3.setFileName("dataFile2.txt");
set3.readDataFile();
set3.printDataSet();
|