So I am trying to open a file and parse a file in C++ (using initializeSimulation()
. I can open the file successfully, but when I try to parse the file and store specific values from the file into my variables, I get the THREAD 1
error. The DataParser.cpp
file is one I am required to use and cannot modify, I am to simply call it. I think the issue is with my function call to getData()
(using pointers) but I cannot seem to figure out why. The way my variables are set up now is working weirdly for some reason, until i try to store m_sMaxVal
. When I set them all up as pointers, I was getting a different error. I'm new to C++ but please let me know if you need any more information, and thanks in advance.
Simulation.cpp
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "Simulation.hpp"
#include "dDataParser.h"
Simulation::Simulation() {}
Simulation::~Simulation(){}
void Simulation::initializeSimulation() {
char *fileName;
char *m_sType;
char m_sMaterial;
int m_iID;
char m_sUnits;
double m_sMinVal;
double *m_sMaxVal;
cout << "Enter the name of the data file:" << endl;
cin >> fileName ;
//cout << fileName << "\n" ;
parser = new EnviroSimDataParser(fileName);
parser ->getSensorData(m_sType, &m_sMaterial, &m_iID, &m_sUnits, &m_sMinVal, m_sMaxVal);
}
DataParser.cpp
EnviroSimDataParser::EnviroSimDataParser(char *fileName)
{
char line[128];
strcpy(m_sFileName, fileName);
inFile = new ifstream();
inFile->open(m_sFileName, fstream::in); // Open the data file
m_iNumSensors = 0; // Number of sensors in the file
m_iNumDisplays = 0; // Number of display devices in the file
m_iNextSensor = 0; // Next sensor number to read
m_iNextDisplay = 0; // Next display number to read
if(inFile->is_open())
{
cout << "file opened successfully1 \n";
// Count the number of sensors
while(getNextLine(line, 127))
{
if(strcmp(line, "<SENSOR>") == 0)
m_iNumSensors++;
if(strcmp(line, "<DISPLAY_DEVICE>") == 0)
m_iNumDisplays++;
}
inFile->close();
cout << "file closed successfully1 \n";
}
else
{
cout << "Failed to open file. Application terminated...\n";
exit(0);
}
}
//-----
bool DataParser::getData(char *type, char *material, int *ID,
char *units, double *minVal, double *maxVal)
{
int sNum = 0;
char line[128];
// See if we have read all sensors
if(m_iNextSensor >= m_iNumSensors) return false;
// Reopen the file
inFile = new ifstream();
inFile->open(m_sFileName, fstream::in);
if(inFile->is_open())
{
// Read to the the current sensor count
while(getNextLine(line, 127))
{
if(strcmp(line, "<SENSOR>") == 0) // Got one
{
if(sNum == m_iNextSensor)
{
// Get data on this one
while(getNextLine(line, 127))
{
// Get the type
if(strcmp(line, "<TYPE>") == 0)
{
if(getNextLine(line, 127))
{
strcpy(type, line); // Set the type
}
else
return false; // Oops!
}
else if(strcmp(line, "<MATERIAL>") == 0)
{
if(getNextLine(line, 127))
{
strcpy(material, line); // Set the material
}
else
return false; // Oops!
}
else if(strcmp(line, "<ID>") == 0)
{
if(getNextLine(line, 127))
{
*ID = atoi(line); // Set the ID
}
else
return false; // Oops!
}
else if(strcmp(line, "<UNITS>") == 0)
{
if(getNextLine(line, 127))
{
strcpy(units, line); // Set the units
}
else
return false; // Oops!
}
else if(strcmp(line, "<MINIMUM_VALUE>") == 0)
{
if(getNextLine(line, 127))
{
*minVal = atof(line); // Set the minimum value
}
else
return false; // Oops!
}
else if(strcmp(line, "<MAXIMUM_VALUE>") == 0)
{
if(getNextLine(line, 127))
{
*maxVal = atof(line); // Set the minimum value
}
else
return false; // Oops!
}
else if(strcmp(line, "</SENSOR>") == 0)
{
m_iNextSensor++; // Increment for next sensor
return true; // Got it
}
} // end while
} // end if(sNum == m_iNextSensor)
else
{
sNum++; // Check the next one
}
}
}
inFile->close();
} // end if file open
return false; // If we get here we have got all the sensors
}
edit: the debugger stops at the line of
*maxVal = atof(line); //Set the minimum value