Another Unhandled Exception Error
Feb 13, 2013 at 1:59am UTC
Hi im trying to make a sudoku game...
What I want to do is make a Sudoku game that will be able to read from a file
and fill the
vector< vector<int > >
respectively but before I worked on the Sudoku class I have started working on a FileHandler class (I know I can just use fstream , ifstream , or ofstream) and I keep getting this Error
Unhandled exception at 0x5A3BF05D (msvcp110d.dll) in Console Sudoku.exe: 0xC0000005: Access violation reading location 0x00000000.
This is the main file:
1 2 3 4 5 6 7 8 9 10 11
#include "FileHandler.h"
int main(int nArgs , char * pszArgs){
FileHandler fh("log.txt" );
fh << "Please Work!" ;
system("PAUSE" );
return EXIT_SUCCESS;
}
This is the function i am trying to call is:
1 2 3
void FileHandler::operator << (char * message){
write(message);
}
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
void FileHandler::write(char * message){
o_Open(); //// opens the file
if (m_ofFile.good() && m_ofFile.is_open() && !m_ofFile.fail())
try {
m_ofFile << message;
if (m_ofFile.bad() || m_ofFile.fail() || !m_ofFile){
char * emessage;
sprintf( emessage, "File %s corrupted" , m_cFileName_ptr);
throw Error( emessage , BAD , 2);
}
m_ofFile.close();
}
catch (Error e){
cerr << e.getMessage() << " ERROR STATUS: " ;
switch (e.getErrorType()){
case OK:{
cerr << "OK" ;
}break ;
case BAD:{
cerr << "BAD" ;
e.displayError();
}break ;
case HORRIBLE:{
cerr << "HORRIBLE" ;
e.displayError();
exit(0);
}break ;
}
}
catch (...){
cerr << "Unknown Error Occured" ;
exit(0);
}
else {
cerr << "Could not write to file" ;
m_ofFile.clear();
}
o_Close(); //// just closes the file w/ m_ofFile.close()
}
Then here is the Open function
1 2 3 4 5 6 7 8 9 10
void FileHandler::o_Open(){
if (m_ofFile.is_open()){
o_Close();
m_ofFile.open(m_cFileName_ptr);
}
else {
m_ofFile.open(m_cFileName_ptr);
}
}
The error object is fine i debugged it and get no problems.
please help i am utterly confused. Thanks in advanced
Feb 13, 2013 at 2:09am UTC
1 2 3
char * emessage // emessage points to some random place in memory.
sprintf( emessage, "File %s corrupted" ,
m_cFileName_ptr); // write to some random place in memory.
However since the error is from reading, I suspect m_cFileName_ptr is also not kosher.
Last edited on Feb 13, 2013 at 2:11am UTC
Feb 13, 2013 at 6:50pm UTC
What do you mean? (in respects to m_cFileName_ptr not being kosher)
here is some more information on that variable:
m_cFileName_ptr
is a char *
and a member variable of class FileHandler
Feb 13, 2013 at 7:48pm UTC
¿what's its value?
By the way, void FileHandler::write(const char * message)
Last edited on Feb 13, 2013 at 7:50pm UTC
Feb 14, 2013 at 6:45pm UTC
The value is set In the constructor
1 2 3
FileHandler::FileHandler(char * location){
setFile(location);
}
1 2 3
void FileHandler::setFile(char * location){
m_cFileName_ptr = location;
}
Topic archived. No new replies allowed.