Click here to Skip to main content
15,902,909 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Updated program-having a pointer error Pin
David Crow4-Nov-09 3:55
David Crow4-Nov-09 3:55 
GeneralRe: Updated program-having a pointer error Pin
kbury4-Nov-09 4:09
kbury4-Nov-09 4:09 
GeneralRe: Updated program-having a pointer error Pin
Ibrahim Bello4-Nov-09 4:18
Ibrahim Bello4-Nov-09 4:18 
GeneralRe: Updated program-having a pointer error Pin
softwaremonkey4-Nov-09 8:29
softwaremonkey4-Nov-09 8:29 
GeneralRe: Updated program-having a pointer error Pin
Ibrahim Bello4-Nov-09 9:50
Ibrahim Bello4-Nov-09 9:50 
GeneralRe: Updated program-having a pointer error Pin
David Crow4-Nov-09 4:24
David Crow4-Nov-09 4:24 
GeneralRe: Updated program-having a pointer error Pin
kbury4-Nov-09 5:08
kbury4-Nov-09 5:08 
QuestionProblem reading from text file Pin
Ibrahim Bello4-Nov-09 3:22
Ibrahim Bello4-Nov-09 3:22 
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 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 ...
AnswerRe: Problem reading from text file Pin
David Crow4-Nov-09 3:51
David Crow4-Nov-09 3:51 
AnswerRe: Problem reading from text file Pin
«_Superman_»4-Nov-09 3:56
professional«_Superman_»4-Nov-09 3:56 
GeneralRe: Problem reading from text file Pin
Ibrahim Bello4-Nov-09 4:22
Ibrahim Bello4-Nov-09 4:22 
AnswerRe: Problem reading from text file Pin
krmed4-Nov-09 7:09
krmed4-Nov-09 7:09 
GeneralRe: Problem reading from text file Pin
Ibrahim Bello4-Nov-09 9:31
Ibrahim Bello4-Nov-09 9:31 
GeneralRe: Problem reading from text file Pin
David Crow4-Nov-09 9:44
David Crow4-Nov-09 9:44 
GeneralRe: Problem reading from text file Pin
Ibrahim Bello9-Nov-09 2:02
Ibrahim Bello9-Nov-09 2:02 
Questionload image Pin
Arefeh Haghpnah4-Nov-09 3:22
Arefeh Haghpnah4-Nov-09 3:22 
AnswerRe: load image Pin
David Crow4-Nov-09 3:33
David Crow4-Nov-09 3:33 
QuestionCan't create new project in VS 6.0 Pin
ephe4-Nov-09 3:09
ephe4-Nov-09 3:09 
QuestionHow do you profile your C++ code ? Pin
Ahmed Charfeddine4-Nov-09 2:04
Ahmed Charfeddine4-Nov-09 2:04 
Questionis it possible to genarate barcode application using vc++6.0 Pin
eswar pothula4-Nov-09 0:42
eswar pothula4-Nov-09 0:42 
AnswerRe: is it possible to genarate barcode application using vc++6.0 Pin
Jan Sommer4-Nov-09 1:46
Jan Sommer4-Nov-09 1:46 
AnswerRe: is it possible to genarate barcode application using vc++6.0 [modified] Pin
Code-o-mat4-Nov-09 2:22
Code-o-mat4-Nov-09 2:22 
QuestionApplication that communicates with a database - how? Pin
Jan Sommer4-Nov-09 0:32
Jan Sommer4-Nov-09 0:32 
AnswerRe: Application that communicates with a database - how? Pin
David Crow4-Nov-09 2:51
David Crow4-Nov-09 2:51 
GeneralRe: Application that communicates with a database - how? Pin
Jan Sommer4-Nov-09 3:09
Jan Sommer4-Nov-09 3:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.