المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Problem reading from text file



C++ Programming
11-05-2009, 11:51 AM
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 ******** 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 Tokens;
int i = 0;
string val;
char c;

ifstream file(filePath); //instantiate and open file

if (!file.fail())
{
//cout > c;
//<span class="code-comment">cout temperature = convertToFloat(Tokens.at(0));
g_ptrMaterial[i]->density = convertToFloat(Tokens.at(1));
g_ptrMaterial[i]->viscosity = convertToFloat(Tokens.at(2));

i++;
}
}
}

else
{
cerr > temp)
{
Tokens.push_back(temp);
}

return Tokens;
}

Debugging
What I did was to attach my debugger to the executable process [Tools -> Attach to Process] as it runs. I noticed that the error is triggered in the GetTokens function. It reads the first row fine i.e (50, 0.2, 0.3), but when it comes to the 2nd row it gets stuck just when about returning Tokens from the GetTokens function. What could be the problem? I guess, I'm out of my depth on this one ...