OK, actually this program works fine, but only when run from Visual Studio. However, when I click on the .exe or launch it from windows command prompt, I get an error: "Unhandled exception at 0x775e59c3 in Materials.exe: 0xC0000005: Access violation reading location 0x54a075d8" and Windows shuts the program down. The Requirement The program is to read from a text file a list of tabulated data of a given material: Temperature, Density, Viscosity... etc. For example: Temperature Density Viscosity ... ... 50 0.2 0.3 ... ... 100 0.25 0.33 ... ... The aim of the program is to read these values (several) of them, store to memory and do some sorts of interpolations. I created a structure, each holding the properties of the material at a given temperature. I then dynamically create an array of structures based on the number of data. If I had 100 readings, I create 100 arrays to structure. .h file
struct Material {
float temperature;
float density;
float viscosity;
};
typedef Material* MATERIAL;
The above go into the header file .cpp file
MATERIAL* g_ptrMaterial; //global variable
void ReadFile (char* filePath)
{
vector<string> Tokens;
int i = 0;
string val;
char c;
ifstream file(filePath); //instantiate and open file
if (!file.fail())
{
//cout << "Enter to begin...";
//cin >> c;
//cout << "Reading from File ...";
g\_numberOfRows = getNumberOfRows(file); //gets number of readings
g\_ptrMaterial = new MATERIAL\[g\_numberOfRows\];
getline(file, g\_fileHeader); //remove column headers i.e. Temperature, Density, etc
while (getline(file, val))
{
g\_ptrMaterial\[i\] = (MATERIAL) malloc(sizeof(MATERIAL));
Tokens = GetTokens(val);
if (!Tokens.empty())
{
//convertToFloat: converts string type to float type
g\_ptrMaterial\[i\]->temperature = convertToFloat(Tokens.at(0));
g\_ptrMaterial\[i\]->density = convertToFloat(Tokens.at(1));
g\_ptrMaterial\[i\]->viscosity = convertToFloat(Tokens.at(2));
i++;
}
}
}
else
{
cerr << "FILE NOT FOUND!";
exit(1);
}
}
//separates the input line into column readings
vector<string > GetTokens (string val)
{
stringstream ss(val);
string temp;
vector<std::string > Tokens;
while (ss >> temp)
{
Tokens.push\_back(temp);
}
return Tokens;
}
Debugging What I di